如何调用与另一个名称相同的成员函数?

时间:2021-03-03 16:46:26

I need two member functions with the same name "functionName" in className class. Like this..

我需要在className类中使用两个名为“functionName”的成员函数。喜欢这个..

className::functionName(BaseClass &object)
{
    //do something here with object
}
className::functionName(SecondBaseClass &object2)
{
    //do something here with object2
}

So, If I have two functions with the same name, how do I call the one that I want?

所以,如果我有两个同名的函数,我该如何调用我想要的函数?

1 个解决方案

#1


1  

This is called function overloading. It picks one depending on the type of the parameter, but it will fail to compile if the parameter you want to pass could reasonably convert to more than one overload.

这称为函数重载。它根据参数的类型选择一个,但如果要传递的参数可以合理地转换为多个重载,则无法编译。

If your parameter is derived from both base classes, try the following to disambiguate it

如果您的参数是从两个基类派生的,请尝试以下方法来消除它的歧义

functionName(static_cast<DesiredBase&>(myobject));

#1


1  

This is called function overloading. It picks one depending on the type of the parameter, but it will fail to compile if the parameter you want to pass could reasonably convert to more than one overload.

这称为函数重载。它根据参数的类型选择一个,但如果要传递的参数可以合理地转换为多个重载,则无法编译。

If your parameter is derived from both base classes, try the following to disambiguate it

如果您的参数是从两个基类派生的,请尝试以下方法来消除它的歧义

functionName(static_cast<DesiredBase&>(myobject));