简体   繁体   中英

Function overloading with the only change in “uint32_t” & uint64_t" in pure virtual function in c++

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM