如何在C ++中继承后覆盖基类的成员 [英] How to override member of base class after inheritance in C++
本文介绍了如何在C ++中继承后覆盖基类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class A {
public:
A(int i):m_S(i)
{
m_Pa = new Foo这个) ;
}
private:
int m_S;
Foo * m_Pa;
}
和派生类
B类:public A {
public:
B():A(242)
{
//这里我喜欢重写A类m_Pa成员,但我不知道如何做到正确
}
}
解决方案您的m_Pa应该受到保护,比您可以调用:
B():A(242),m_Pa(12)
{
}
或
(242)
{
m_PA = 55
}
你应该创建一个公共或保护的函数来改变m_Pa
class A {
public:
A int i):m_S(i)
{
m_Pa = new Foo(* this);
}
void setPA(int val)
{
m_PA = val;
}
class A { public : A(int i ) : m_S(i) { m_Pa = new Foo(*this) ; } private : int m_S ; Foo* m_Pa; } and derived class class B : public A { public : B() : A (242) { // here i like to override the A class m_Pa member but i don't know how to do it right } }
解决方案your m_Pa should be protected than you can call like:
B() : A (242), m_Pa(12) { }
or
B() : A (242) { m_PA = 55 }
or you should make a public or protected function which changes m_Pa
class A { public : A(int i ) : m_S(i) { m_Pa = new Foo(*this) ; } void setPA(int val) { m_PA = val; }
这篇关于如何在C ++中继承后覆盖基类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文