我不明白爲什麼在這種情況下複製構造函數沒有被調用。有人可以解釋一下嗎?複製構造函數不叫!
#include <iostream>
class foo
{
int* ptr;
public:
foo()
{
std::cout << "\n Constructor \n" ;
ptr = new int;
*ptr = 10;
}
foo(const foo* &obj) // Copy Constructor
{
std::cout << "\n Copy Constructor \n" ;
ptr = new int;
*(this->ptr) = *(obj->ptr);
}
// Copy Assignment Operator
~foo() // Destructor
{
delete ptr;
}
};
int main()
{
foo* objOne = new foo;
foo* objTwo = objOne ;
getchar();
return 0;
}
你爲什麼不自己嘗試一下? ;) – visitor 2011-02-11 09:54:20