class Sample
{
public:
Sample(){}
Sample(const Sample& obj){ cout<<"C.C. with 1 argument called"<<endl;}
Sample(const Sample& obj, int i){ cout<<"C.C. with 2 arguments called"<<endl;}
};
void main()
{
Sample s1;
Sample s2 = s1; // Here, C.C with 1 arg. called.
}
有幾個問題:複製構造與2參數
- 我如何撥打電話,以複製爲2個參數的構造函數?
- 當我們需要一個具有1個參數的複製構造函數,並且當我們需要具有2個參數的C.C時?
樣本s2 = s1; //這裏,C.C和1個參數。調用。您在此處呼叫賦值運算符。 – 2012-03-20 16:18:53
@AlessandroTeruzzi:不,這是調用複製構造函數。 – 2012-03-20 16:23:19
@BenjaminLindley你是對的!我從來沒有意識到這相當於樣本s2(s1);是標準還是編譯器優化? – 2012-03-20 16:33:36