由于多态性(我猜),我有这个编译错误
错误:没有匹配的函数可用于调用‘衍生::print()’
:
#include <iostream>
class base{
public:
void print()
{
print("Hello from ");
}
private:
virtual void print(const std::string & message)
{
std::cout << message << " base class !! \n";
}
};
class derived: public base{
private:
void print(const std::string & message) override
{
std::cout << message << " derived class !! \n";
}
};
int main()
{
base b;
b.print();
derived d;
d.print();
return 0;
}
不重命名方法
print
且不在派生类中重复它的最佳解决方案是什么?
没有重命名,没有重复:)
int main()
{
base b;
b.print();
derived d;
d.base::print();
return 0;
}