简体   繁体   English

c ++继承:与派生对象的基本成员访问混淆

[英]c++ inheritance : confused with base member access from derived object

Declaration: 宣言:

class base{
public:
    base(){a=0;}
    base(int a);
    int getA();
    virtual ~base() ;
protected:
    int a ;
};
//
class derived : public base {
public:
    derived(){}
    derived(int a, int b) ;
    int getC() ;
    ~derived();
private:
    int c ;
};

Definition : 定义:

base::base(int a){
    cout << "   in base constructor a = " << a << endl ;
    a = a ;
}
base::~base(){
    a = 0 ;
}
int base::getA(){
    return a ;
}
//
derived::derived(int a, int b)
:base(a)
{
    c = a+b ;
}
int derived::getC()
{
    return c ;
}
derived::~derived(){
    c = 0 ;
}

Call: 呼叫:

derived A(4,5) ;
cout << "   a of A = " << A.getA() << "\n" ;
cout << "   c of A = " << A.getC() << "\n" ;

Result : 结果:

in base constructor a = 4
a of A = 4205648
c of A = 9

Please can someone explain why I am getting this result instead of : 请有人能解释为什么我得到这个结果而不是:

a of A = 4 

? Why does the value of base member a change ? 为什么基本成员的值发生变化? Based on what I've learned about inheritance in c++, when we create an object of derived class, this object will inherit all members and member functions of base class, why is it that the member a of object A of class derived loses its value outside the derived class definition ? 根据我对c ++继承的了解,当我们创建派生类的对象时,该对象将继承基类的所有成员和成员函数,为什么派生类的对象A的成员a失去其值在派生类定义之外?

Thanks ! 谢谢 !

base::base(int a){
    cout << "   in base constructor a = " << a << endl ;
    a = a ;
}

This line changes the value of constructor parameter a , not the value of a of base . 这行更改了构造函数参数a的值,而不是basea值。 Better will be 会更好

base::base(int _a){
    cout << "   in base constructor a = " << _a << endl ;
    a = _a ;
}

And even better is to use constructor initialization list: 更好的是使用构造函数初始化列表:

base::base(int _a): a(_a) {
    cout << "   in base constructor a = " << a << endl ;
}

In the latter case you can even write 在后一种情况下,您甚至可以编写

base::base(int a): a(a) {
    cout << "   in base constructor a = " << a << endl ;
}

because in the initialization list there is no ambiguity, although I still prefer to be explicit and use different names for constructor parameters and class members. 因为在初始化列表中没有歧义,尽管我仍然更喜欢明确一些,并为构造函数参数和类成员使用不同的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++多重继承,与派生对象中基类的地址混淆 - C++ multiple inheritance, confused with addresses of base classes in derived object C++ 从基类指针访问派生类成员 - C++ Access derived class member from base class pointer 在C ++中,是否可以从同级第二个派生对象访问第一个派生对象的受保护基础数据成员? - In C++ is it possible to access a 1st derived object's protected base data member from a sibling 2nd derived object? C ++派生类是否可以从Base类继承静态数据成员和静态成员函数? - C++ Does derived class could inheritance Static data member and Static Member function from Base class? 基类和派生类之间的继承数据成员c ++ - inheritance data member between base and derived class c++ C ++继承基类使用派生类的成员 - C++ inheritance Base class uses member of derived class C++ 中的 Inheritance,如何从派生的 ZA2F2ED4F8EBC2CBB14C21A29DC40 中初始化基础 class 中的成员变量 - Inheritance in C++, how to initialize member variables in base class from derived class 如何从派生类访问派生基成员?(在C ++中) - How can I access derived base member from derived class?(in C++) C ++将私有成员从派生类访问到另一个派生类(两者都具有相同的基类) - C++ Access private member from a derived class to another derived class (both have the same base class) C ++多重继承和访问说明符:从基类及其派生继承 - C++ multiple inheritance and access specifiers: inherit from a base class and its derived
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM