简体   繁体   English

具有多态性的复制构造函数和赋值运算符

[英]Copy constructor and assignment operators with polymorphism

Just failed an interview because of this, and I'm still confused:因为这个而面试失败了,我仍然很困惑:

class A
{
public:
    A(int a) : a_(a) {}
    
    // Copy constructor
    
    // Assignment operator
    
private:
    int a_;
};
 
class B : public A
{
public:
    B(int a, int b) : A(a), b_(b) {
    }
    
    // Copy constructor
    
    // Assignment operator
    
private:
    int b_;
};

How do you implement the copy constr/assignment op, and why does the constructor of B have to have an A initialized in the initializer list?你如何实现复制构造/赋值操作,为什么 B 的构造函数必须在初始化列表中初始化 A? It doesn't work if it's not in the list.如果它不在列表中,则它不起作用。

why does the constructor of B have to have an A initialized in the initializer list?为什么 B 的构造函数必须在初始化列表中初始化 A?

A only has one constructor which takes an int. A只有一个采用 int 的构造函数。 B has A as a base class, which means that every B object must first construct an A object. BA作为基础class,这意味着每个B object必须首先构造一个A object。 How else are you going to construct that A object with it's required parameter except by using an initialiser list?除了使用初始化列表之外,您还打算如何构造带有所需参数A object ?

How do you implement the copy constr/assignment op你如何实现复制构造/赋值操作

Trick question, I think.诡计问题,我想。 In this case the best option is to do absolutely nothing.在这种情况下,最好的选择是什么都不做。 Neither class contains (and owns ) any resources requiring more than the default special member functions. class 都不包含(并拥有)任何需要超过默认特殊成员函数的资源。 Observe the Rule of Zero .遵守零规则

Some style guides recommend explicitly defaulting special member functions.一些样式指南建议显式默认特殊成员函数。 In this case在这种情况下

class A
{
public:
    A(int a) : a_(a) {}
    
    A(const A &) = default;
    A& operator=(const A &) = default;
    
private:
    int a_;
};

may be appropriate.可能是合适的。

why does the constructor of B have to have an A initialized in the initializer list?为什么 B 的构造函数必须在初始化列表中初始化 A?

All class members and base classes must be fully constructed before entering the body of a constructor.所有 class 成员和基类必须在进入构造函数的主体之前完全构造。

B(int a, int b) 
{ // Entering constructor's body. A must be constructed before we get here.
}

A has no default constructor, so the constructor it does have must be explicitly called in the initializer list to construct the base class before the construction of B can proceed. A没有默认构造函数,因此必须在初始值设定项列表中显式调用它所具有的构造函数来构造基础 class,然后才能继续构造B

Addressing comment解决评论

A 's copy constructor and assignment operator are trivial: A的复制构造函数和赋值运算符是微不足道的:

A(const A & src): a_(src.a_)
{

}

A& operator=(const A & src)
{
    a_ = src.a_;
    return *this;
}

B is a bit trickier because it also has to make sure A is copied B有点棘手,因为它还必须确保A被复制

B(const B & src): A(src), // copy the base class
                  b_(src.b_)
{

}

B& operator=(const B & src)
{
    A::operator=(src); // assign the base class by explicitly calling its 
                       // assignment operator
    b_ = src.b_;
    return *this;
}

Note: If I were hiring, I'd take the programmer who called me out on the trick question over the programmer who did the extra work and risked an unforced error.注意:如果我正在招聘,我会选择那个在技巧问题上叫我出来的程序员,而不是那些做额外工作并冒着非受迫性错误风险的程序员。

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

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