Overriding a Base#39;s Overloaded Function in C++(在 C++ 中重写基的重载函数)
问题描述
Possible Duplicate:
C++ overload resolution
I ran into a problem where after my class overrode a function of its base class, all of the overloaded versions of the functions were then hidden. Is this by design or am I just doing something wrong?
Ex.
class foo
{
public:
foo(void);
~foo(void);
virtual void a(int);
virtual void a(double);
};
class bar : public foo
{
public:
bar(void);
~bar(void);
void a(int);
};
the following would then give a compile error saying there is no a(double) function in bar.
main()
{
double i = 0.0;
bar b;
b.a(i);
}
In class bar, add
using foo::a;
This is a common 'gotcha' in C++. Once a name match is found in the a class scope, it doesn't look further up the inheritance tree for overloads. By specifying the 'using' declaration, you bring all of the overloads of 'a' from 'foo' into the scope of 'bar'. Then overloading works properly.
Keep in mind that if there is existing code using the 'foo' class, its meaning could be changed by the additional overloads. Or the additional overloads could introduce ambiguity and and the code will fail to compile. This is pointed out in James Hopkin's answer.
这篇关于在 C++ 中重写基的重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中重写基的重载函数


基础教程推荐
- C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
- C语言3个整数的数组 1970-01-01
- 用指数格式表示浮点数 1970-01-01
- 总计将在节日礼物上花多少钱 1970-01-01
- 对 STL 容器的安全并行只读访问 2022-10-25
- 迭代std :: bitset中真实位的有效方法? 2022-10-18
- C语言数组 1970-01-01
- C++多态 1970-01-01
- 向量<unique_ptr<A>>使用初始化列表 2022-10-23
- 明确指定任何或所有枚举数的整数值 1970-01-01