多次调用的C++复制构造函数
C++ copy constructor called multiple times
我试图了解复制构造函数的基础知识并遇到了以下示例。
#include <iostream>
using namespace std;
class Line
{
public:
int getLength() const;
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
// allocate memory for the pointer;
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}
Line::~Line()
{
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength() const
{
return *ptr;
}
void display(Line obj)
{
cout << "Length of line : " << obj.getLength() <<endl;
}
// Main function for the program
int main( )
{
Line line1(10);
Line line2 = line1; // This also calls copy constructor
//display(line1);
//display(line2);
return 0;
}
执行后,输出如下。
Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!
拷贝构造函数只被调用了一次,在Line line2 = line1;
但是好像拷贝构造函数被调用了两次。所以我评论了
display(line1);
和 display(line2);
。之后输出是这样的。
Normal constructor allocating ptr
Copy constructor allocating ptr.
Freeing memory!
Freeing memory!
所以问题(我不知道我应该把它称为问题还是它是 C++ 中的默认标准)与显示功能有关。难道是因为 display() 函数自动创建了其处理对象的副本,这就是为什么每个实例都调用复制构造函数两次?请澄清。谢谢。
Could it be because the display() function automatically creates a copy of the object its processing and that's why the copy constructor is called twice for each instance? Kindly clarify. Thanks.
这正是正在发生的事情。如果按值将对象传递给函数,它将在函数调用执行之前被复制。如果您不希望发生这种情况,请使用指针或引用。
对于 void display(Line obj)
,您按值传递对象,因此创建副本。
您应该通过 (const) 引用以避免复制:
void display(const Line& obj)
我试图了解复制构造函数的基础知识并遇到了以下示例。
#include <iostream>
using namespace std;
class Line
{
public:
int getLength() const;
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
// allocate memory for the pointer;
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}
Line::~Line()
{
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength() const
{
return *ptr;
}
void display(Line obj)
{
cout << "Length of line : " << obj.getLength() <<endl;
}
// Main function for the program
int main( )
{
Line line1(10);
Line line2 = line1; // This also calls copy constructor
//display(line1);
//display(line2);
return 0;
}
执行后,输出如下。
Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!
拷贝构造函数只被调用了一次,在Line line2 = line1;
但是好像拷贝构造函数被调用了两次。所以我评论了
display(line1);
和 display(line2);
。之后输出是这样的。
Normal constructor allocating ptr
Copy constructor allocating ptr.
Freeing memory!
Freeing memory!
所以问题(我不知道我应该把它称为问题还是它是 C++ 中的默认标准)与显示功能有关。难道是因为 display() 函数自动创建了其处理对象的副本,这就是为什么每个实例都调用复制构造函数两次?请澄清。谢谢。
Could it be because the display() function automatically creates a copy of the object its processing and that's why the copy constructor is called twice for each instance? Kindly clarify. Thanks.
这正是正在发生的事情。如果按值将对象传递给函数,它将在函数调用执行之前被复制。如果您不希望发生这种情况,请使用指针或引用。
对于 void display(Line obj)
,您按值传递对象,因此创建副本。
您应该通过 (const) 引用以避免复制:
void display(const Line& obj)