C++子类不能调用父类同名不同参函数
2013-01-18 14:51
375 查看
才知道这件事情,太土了……这样一比,objective-c 比C++ 又不知方便、好用多少!
如下代码:
编译是通不过的,它会说 Child 没有接收 char * 型的 foo 函数,但事实上父类中有!
修改方法:
1. c->Father::foo("yes")
2. 把父类中的 int foo(const std::string& s) 改名为 int foo2(const std::string& s),然后 c->foo2("yes")
3. Child 继承 int foo(const std::string& s) { return __super::foo(s); }
如下代码:
// testSubclass.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> class GrandFather { public: virtual bool foo(int a) = 0; virtual int foo(const std::string &s) = 0; }; class Father : public GrandFather { public: virtual bool foo(int a) { return false; } virtual int foo(const std::string &s) { return 0; } }; class Child: public Father { public: virtual bool foo(int a) { return true; } }; int _tmain(int argc, _TCHAR* argv[]) { Child *c = new Child; c->foo("yes"); return 0; }
编译是通不过的,它会说 Child 没有接收 char * 型的 foo 函数,但事实上父类中有!
修改方法:
1. c->Father::foo("yes")
2. 把父类中的 int foo(const std::string& s) 改名为 int foo2(const std::string& s),然后 c->foo2("yes")
3. Child 继承 int foo(const std::string& s) { return __super::foo(s); }
相关文章推荐
- c++子类和父类同名成员函数
- 父类和子类的同名成员函数参数格式相同,返回值类型不同
- C++子类调用父类覆盖的函数
- C++子类对象隐藏了父类的同名成员函数(隐藏篇)
- C++ 调用父类同名函数
- C++子类调用父类覆盖的函数
- Go语言:同个包下不同文件不能互相调用函数
- C程序使用不同函数调用约定调用汇编子过程
- 父类与子类之间静态函数的调用以及函数参数的传递
- matlab的plot调用函数不能用了,总出现错误:Attempt to execute SCRIPT plot as a function
- RGB 转换 Ycbcr 调用 C++和MATLAB中的函数,得到了两种不同的运算结果
- 父类中调用自身定义的虚函数,其中若修改派生类定义的变量,无效
- lazarus中不同模式下自定义函数的调用方式区别
- C语言学习4: 函数返回值与传入参数,关于函数值传递和类型隐性转换,变量不同的作用域,static变量,多文件编译例如两个C文件,显示函数调用语句跳转,递归,斐波那契数列,多文件编译相同变量的问题。
- 已定义的函数有返回值,函数调用可以作为一个函数的实参,但是不能作为形参
- 父类的构造函数中不能调用纯虚函数,同样在析构函数中不能调用纯虚函数
- 重定义不同基类的同名虚函数
- 绝不能在构造函数与析构函数中调用virtual 函数
- 调用不同dll中的同一函数
- 父类与子类析构与函数调用关系