[英]C++ behavior of a default(implicit) copy constructor in a derived class
我對默認副本構造函數的行為有疑問。 例如,如下所示的類:
class A{
public:
A(){}
A(const A& a){}
~A(){}
void foo(){}
};
class B:public A{
public:
B(){}
B(const B& b){}
B& operator=(const B& b){return *this;}
~B(){}
virtual void foo(){}
};
class C:public B{
public:
C(){}
C& operator=(const C& c){a_=c.a_; return *this;}
~C(){}
void foo(){}
protected:
A a_;
};
如果我將創建一個新的類C的對象,例如:
C* c1 = new C();
步驟將是:
如果我要像這樣初始化C類的新對象:
C c2(*c1);
它會調用C的默認副本構造函數。據我所知,這些步驟將是:
默認副本構造函數的行為如何? 它有什么規則?
我嘗試在Internet上搜索默認副本構造函數的實現,但沒有找到解釋此行為的內容。 有人可以建議一些有關此問題的信息嗎?
當您有派生類復制構造函數時,例如...
C(const C& c) {...}
您可能會認為這會自動調用A和B的復制控制器,但不會。 隱式行為就像您已經寫過...
C(const C& c) : B() {...}
...然后B的B()做...
B() : A() {...}
如果要在基類中調用復制ctor,則需要顯式指定這種行為,例如...
C(const C& c) : B(c) {...}
隱式生成的復制控制器已經為您完成了此操作。
據您所見,在您所處的情況下, operator=
被調用的事實並非如此 。 我不知道你為什么會這樣。
編譯運行,檢查代碼中的注釋,將會更加清晰:
#include <iostream>
class A{
public:
A()
{
}
A(const A& a)
{
std::cout << "Copy constructor FOR A is being called" << std::endl;
}
virtual ~A(){}
void foo(){}
};
class B : public A
{
public:
B()
{
}
B(const B& b)
{
std::cout << "Copy constructor FOR B is being called" << std::endl;
}
B& operator=(const B& b){return *this;}
virtual ~B()
{
}
virtual void foo(){}
};
class C : public B
{
public:
C()
{
}
//if you remove this copy constructor, instead of only C being called, both A and B's copy constructor will be called
C(const C& c)
{
std::cout << "Copy constructor FOR C is being called" << std::endl;
}
C& operator()(const C& c)
{
std::cout << "Operator is being called" << std::endl;
a_=c.a_;
return *this;
}
~C()
{
}
void foo()
{
}
protected:
A a_;
};
int main()
{
C* c = new C();
C c2(*c); //copy constructor C will be called since we declared one, otherwise both A's and B's copy constructor would be called
c2(c2); //here the operator() will be called but not above, changed that one to operator() since operator= didn't make sense
delete c;
std::cin.get();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.