简体   繁体   English

函数重载,只有c ++中的纯虚函数中的“ uint32_t”和uint64_t”发生了变化

[英]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. 我在c ++类中定义了一对纯虚函数,我已经重载了。 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 ".. 在这里,函数签名的唯一区别是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 . 在c ++中,onverloading仅在函数的参数上执行,而不在返回类型上执行 So your are redefining the same function which is an error. 因此,您正在重新定义相同的功能,这是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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