0

I've a pair pure virtual function defined inside a c++ class, which I've overloaded. Code snippet below::

virtual uint64_t Abc::GetValue()=0; 
virtual uint32_t Abc::GetValue()=0;

Here, the only difference in function signature is the return type of GetVal() "uint32_t" & "uint64_t"..

It's throwing compilation error that this can't be overloaded.

Plz help me on this.

WhozCraig
  • 63,603
  • 10
  • 71
  • 134
codeLover
  • 3,623
  • 10
  • 58
  • 115

2 Answers2

3

You can't overload based on return type, only on parameter types. This is because the overload is chosen based on how the function is called, and a function call doesn't specify the expected return type.

Options include:

  • Give the functions different names;
  • "return" the value via a reference parameter, rather than the return value;
  • add a dummy parameter, with a different type for each version, to specify the return type;
  • return a "proxy" object that's convertible to either type (although this means you'll only have a single function, so derived classes can't override the two versions separately).
Mike Seymour
  • 242,813
  • 27
  • 432
  • 630
1

In c++ onverloading is only done on the parameters of the function not on the return type. So your are redefining the same function which is an error.

hivert
  • 10,396
  • 3
  • 29
  • 55