-1
規則三。複製構造函數,賦值運算符執行法則三。複製構造函數,賦值運算符實現
#include <iostream>
using namespace std;
class IntPart
{
public:
IntPart(); // default constructor
IntPart(int n);
private:
unsigned int* Counts;
unsigned int numParts;
unsigned int size;
};
IntPart::IntPart()
{
Counts = new int[101](); // allocate all to 0s
numParts = 0;
}
IntPart::IntPart(int n)
{
Counts = new int[n+1](); // allocate all to 0s
Counts[n] = 1;
numParts = 1;
}
int main()
{
IntPart ip2(200);
IntPart ip3(100);
IntPart ip(ip2); // call default and copy constructor?
IntPart ip4; // call default constructor
ip4 = ip3;
system("pause"); return 0;
}
很明顯,這需要有三條規則。 你能幫我定義他們嗎?
Q0。
IntPart ip(ip2);
這是否一個科瑞IP對象調用默認的構造函數 ,之後,調用拷貝構造函數? 我對不對?
Q1。定義析構函數。
IntPart::~IntPart()
{ delete [] Counts; }
它正確嗎? Q2302。定義複製構造函數。
IntPart::IntPart(const IntPart& a)
{ // how do I do this? I need to find the length... size.... could anybody can do this?
}
Q3。定義賦值運算符。
IntPart& IntPart::operator= (const IntPart& a)
{
if (right != a)
{
// Anybody has any idea about how to implement this?
}
return *this;
}
謝謝, 我將不勝感激!
字面上有數百萬*樣本對象實現遵循網絡上的三規則。數千在這個站點單獨。看到右邊的「相關」列表?嘗試點擊它。 VTC。 – WhozCraig 2013-04-23 06:39:21
如果需要可能爲'Counts'分配空間,並從'a.Counts'複製。可能首先刪除舊的「Counts」(如果它太小)。 – 2013-04-23 06:42:01
可能重複[三條法則是什麼?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-ree) – 2013-04-23 07:23:51