简体   繁体   English

将Object初始化为另一个类的成员

[英]Initialize Object as member of another class

I just want to understand the flow how it happens as am new to c++. 我只是想了解它是如何发生的,因为它是c ++的新手。

Let me elaborate my problem statement. 让我详细说明我的问题陈述。

class test1 {
public:
    test1() {
        cout << " test1::constructor called" << endl;
    }
    ~test1() {
        cout << " test1::destrcutor called" << endl;
    }
};

class test2 {
public:
    test2() {
        cout << " test2::constructor called" << endl;
    }
    ~test2() {
        cout << " test2::destrcutor called" << endl;
    }
};

class derived :public test1, public test2 {
    test2 t2;
    test1 t1;
public:
    derived() {
        cout << " derived constructor called" << endl;
    }
    ~derived() {
        cout << "derived destructor called" << endl;
    }
};

int main() {
    derived d;
    return 0;
}

The output of the above program shows 上述程序的输出显示

   test1::constructor called
   test2::constructor called
   test2::constructor called
   test1::constructor called
   derived constructor called
   derived destructor called
   test1::destrcutor called
   test2::destrcutor called
   test2::destrcutor called
   test1::destrcutor called

So here my question is at what points it called the constructor of the member variables in derived class as I have not put any initializer for the same. 所以这里我的问题是在什么点上它称为派生类中成员变量的构造函数,因为我没有为它设置任何初始化程序。

The order of construction is bases, then members so :- 建筑的顺序是基础,然后成员如此: -

test1::constructor called  << first base of 'derived'
test2::constructor called  << second base of 'derived'
test2::constructor called  << member of 'derived' t2
test1::constructor called  << member of 'derived' t1
derived constructor called << code in 'derived::derived'
derived destructor called  << code in 'derived::~derived'
test1::destrcutor called   << destruction of t1
test2::destrcutor called   << destruction of t2
test2::destrcutor called   << destruction of derived
test1::destrcutor called   << destruction of derived

There is only one destructor of an object, and it has a defined order to destroy objects. 对象只有一个析构函数,它有一个定义的命令来销毁对象。 That is destroy all the members from bottom to top of the class. 这会破坏班级从头到尾的所有成员。

Then destroy the class, and its bases in "reverse order". 然后以“逆序”破坏阶级及其基础。

Each constructor can choose what to initialize, but not the order. 每个构造函数都可以选择要初始化的内容,但不能选择顺序。

a_class::a_class( params ) : 
           base_n( params ), base_n_1( params ), 
           member_1( params), member_2( params),...

This member initialization list allows different parameters to be given to construct all the bases and objects, but does not effect the order. member initialization list允许给出不同的参数来构造所有的基础和对象,但不影响顺序。 It is always first_base , second_base , first_member , second_member , ... 它始终是first_basesecond_basefirst_membersecond_member ,...

This ordering is to ensure it is the opposite of the destructor. 这种排序是为了确保它与析构函数相反。

These rules allowed me to work out which message was from the members, and which from the bases. 这些规则允许我弄清楚哪些消息来自成员,哪些来自基地。

A member which does not get initialized from the member initialization list will still get its default constructor called test2::test2 . 未从member initialization list初始化的member initialization list仍将获得名为test2::test2默认构造函数。 As once a class / struct have a constructor, they will only come into existence by having a constructor called. 一旦class / struct具有构造函数,它们将仅通过调用构造函数而存在。

Plain-old-Data or POD are simple types such as int which don't have constructors. Plain-old-DataPOD是简单类型,例如int ,它没有构造函数。 They are left uninitialized (whatever values were left behind in the memory). 它们未被初始化(无论在记忆中留下什么价值)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM