简体   繁体   English

定义的副本构造函数c ++的默认行为

[英]default behaviour of defined copy constructor c++

suppose I define a copy c'tor for a class B that inherits A and has a also member variables. 假设我为类B定义了一个复制c'tor,它继承了A并且还具有成员变量。 Inside the copt c'tor body I write some code, but in the initiallization list I don't call explicitly to A c'tor(also not to copy c'tor), and not initiallize the member variables. 在copt c'tor主体内部,我编写了一些代码,但是在初始化列表中,我没有显式调用A c'tor(也不是复制c'tor),也没有初始化成员变量。

In this case A default c'tor will be called defaultly before reaching B' copy c'tor body. 在这种情况下,默认的c'tor将在到达B'复制c'tor主体之前被默认调用。 But what would be with the member variables? 但是成员变量会是什么呢? would they be initillized with their default c'tor or with their copy c'tor with the arguement object's members (the argument object == given to B copy c'tor). 他们会使用其默认c'tor或带有争论对象成员的副本c'tor(参数对象==给予B复制c'tor)进行初始化。

In addition, if there is calling inside the initiallization list to some members' copy c'tor/c'tor or to Parent class copy c'tor/c'tor - would that behaviour be changed? 另外,如果在初始化列表中调用了某些成员的副本c'tor / c'tor或父类副本c'tor / c'tor,该行为是否会改变?

I know that in case we don't define explicitly copy c'tor: Parent Class and members copy c'tor's are called. 我知道,万一我们没有明确定义复制c'tor的话,就会调用父类和复制c'tor的成员。

What should I expect in here? 我在这里应该期待什么?

The base class subobject will be default initialized in that case. 在这种情况下,将默认初始化基类的子对象。 See [class.base.init]/8: 参见[class.base.init] / 8:

In a non-delegating constructor, if a given potentially constructed subobject is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer ), then 在非委托的构造函数中,如果给定的可能构造的子对象未由mem-initializer-id指定(包括由于构造函数没有ctor-initializer而没有mem-initializer-list的情况),则

  • if the entity is a non-static data member that has a brace-or-equal-initializer [..] 如果实体是具有大括号或相等初始化程序 [..]的非静态数据成员,则
  • otherwise, if the entity is an anonymous union or a variant member (9.5), no initialization is performed; 否则,如果实体是匿名联合或变量成员(9.5),则不执行初始化;
  • otherwise, the entity is default-initialized (8.5). 否则,该实体为默认初始化(8.5)。

And a potentially constructed subobject is defined in [special]/5: 在[special] / 5中定义了一个可能构造的子对象:

For a class, its non-static data members, its non-virtual direct base classes , and, if the class is not abstract (10.4), its virtual base classes are called its potentially constructed subobjects. 对于一个类,其非静态数据成员, 其非虚拟直接基类 ,以及如果该类不是抽象(10.4),则其虚拟基类称为其潜在构造的子对象。

If you write a copy-constructor and do not initialize a member variable then it will be default-initialized. 如果您编写一个复制构造函数并且不初始化成员变量,那么它将被默认初始化。

The same applies to base classes, in most ways they are treated the same as member variables. 这同样适用于基类,在大多数情况下,它们与成员变量相同。

Here is some sample code that will demonstrate: 这是一些示例代码,将演示:

#include <iostream>

using namespace std;

struct S
{
    S() { cout << "S()\n"; }
    S(S const &) { cout << "S(S&)\n"; }
};

struct T : S
{
    T() {}
    T(T const &t) {}

    // you have to explicitly write this if you want it 
    // T(T const &t): S(t) {}
    //                ^^^^
};

int main()
{
    T t;
    T u(t);
}

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

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