本文介绍了为什么我因虚拟功能而出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Base
{
private:
int m_age;
protected:
int m_rank;
public:
Base() { cout <<"Base created"<<endl;}
//virtual // Ensures to invoke actual object destructor
~Base() { cout<<"Base destroyed"<<endl;}
virtual void DisplayAction();
};
class Derived1 : public Base
{
public:
Derived1()
{
cout << "Derived1 created" << endl;
}
~Derived1()
{
cout << "Derived1 destroyed" << endl;
}
//void setage(int m){ m_age=m;}
void setrank(int m){ m_rank=m;}
void DisplayAction(){cout <<" rank= "<<m_rank;}
};
推荐答案
virtual void DisplayAction(){cout << "(Base) rank= " << m_rank;}
}; // end of Base
或使其成为纯虚拟,例如
or make it pure virtual, e.g.
virtual void DisplayAction() = 0;
}; // end of Base
这篇关于为什么我因虚拟功能而出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!