弱弱问下解决方案
弱弱问下
Which virtual function redeclarations(声明) of the Derived class are correct?
a. Base* Base::copy(Base*);
Base* Derived::copy(Derived*);
b. Base* Base::copy(Base*);
Derived* Derived::copy(Base*); //oerrided the base
c. ostream& Base::print(int,ostream&=cout);
ostream& Derived::print(int,ostream&);
d. void Base::eval() const;
void Derived::eval();
请大家帮助一下,b在VC60下编译没有通过,其他都编译通过,运行也没有问题,不知道题中的redeclarations具体表示什么含义!谢谢解答!^_^
------解决方案--------------------
b的错误在于: 参数相同,返回值不同。
例如下面的代码
Base * pBase = NULL;
Derived * pDerived = new Derived(...);
pDerived->copy(pBase); // 请问,编译器应该调用哪一个函数呢? Base::copy还是 Derived::copy ?
override 一个函数,必须是参数相同,返回值也相同.
overload 一个函数,则必须参数表不同。
对于参数表相同,返回值不同的声明,编译器无法识别。
redeclaration 从字面意义上看估计是 重新声明 (感觉应该类似于 override)
Which virtual function redeclarations(声明) of the Derived class are correct?
a. Base* Base::copy(Base*);
Base* Derived::copy(Derived*);
b. Base* Base::copy(Base*);
Derived* Derived::copy(Base*); //oerrided the base
c. ostream& Base::print(int,ostream&=cout);
ostream& Derived::print(int,ostream&);
d. void Base::eval() const;
void Derived::eval();
请大家帮助一下,b在VC60下编译没有通过,其他都编译通过,运行也没有问题,不知道题中的redeclarations具体表示什么含义!谢谢解答!^_^
------解决方案--------------------
b的错误在于: 参数相同,返回值不同。
例如下面的代码
Base * pBase = NULL;
Derived * pDerived = new Derived(...);
pDerived->copy(pBase); // 请问,编译器应该调用哪一个函数呢? Base::copy还是 Derived::copy ?
override 一个函数,必须是参数相同,返回值也相同.
overload 一个函数,则必须参数表不同。
对于参数表相同,返回值不同的声明,编译器无法识别。
redeclaration 从字面意义上看估计是 重新声明 (感觉应该类似于 override)