多次调用复制构造函数c++; 我通过C++教程中的复制构造函数教程进行了讲解。
在其中一个示例代码中:多次调用复制构造函数c++; 我通过C++教程中的复制构造函数教程进行了讲解。,c++,copy-constructor,C++,Copy Constructor,在其中一个示例代码中: #include <iostream> using namespace std; class Line { public: int getLength(void); Line(int len); // simple constructor Line(const Line &obj); // copy constructor ~Line(); // destructor
#include <iostream>
using namespace std;
class Line
{
public:
int getLength(void);
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(void)
{
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength(void)
{
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;
}
我不理解输出。对我来说,它显示了为line1调用普通构造函数,然后为line2调用一个复制构造函数,然后为2个对象调用2*“释放内存”
我认为结果是:
Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Length of line : 10
Freeing memory!
Freeing memory!
q、 1>为什么最初多次调用复制构造函数
q、 2>4次“释放记忆”和那次之间的一次,我真的很困惑,你能帮我吗
感谢无效显示(行obj)
为其参数调用复制构造函数,您可以使用const reference来避免复制(
无效显示(const Line&obj)
)。这是简单的构造:
Line line1(10);
正如您所认识到的,这就是调用复制构造函数的地方:
Line line2 = line1;
到目前为止还不错。现在看一下要显示的签名:
void display(Line obj);
这就是我们所说的传递值。passby value是一种参数形式,它导致从传入的对象构造新对象。因此,这里有两个电话:
display(line1);
display(line2);
都在调用复制构造函数将line1
和line2
放入函数localobj
变量中
这大致相当于:
// Instead of calling display, this happens instead:
{ // Entering a new scope
Line obj = line1;
cout << "Length of line : " << obj.getLength() << endl;
} // Exiting scope
{ // Entering a new scope
Line obj = line2;
cout << "Length of line : " << obj.getLength() << endl;
} // Exiting scope
//不调用display,而是发生以下情况:
{//正在输入新范围
线路obj=线路1;
cout示例如下:
第1行(10);//分配ptr的普通构造函数
line2=line1;//复制分配ptr的构造函数
显示(第1行);/*复制构造函数分配ptr,因为函数如下:
无效显示(行obj)
这将在方法中创建对象的副本,就像它是int一样
无效显示(常数线和obj)
它传递一个引用。
*/
/*接下来调用函数并打印
线路长度:10
然后在函数结束时调用副本的析构函数,然后再返回
释放记忆
*/
显示(第2行);/*接下来,在第二次函数调用中重复相同的模式:
复制配置ptr的构造函数
线路长度:10
释放记忆!
*/
返回0;/*最后,将销毁第1行和第2行:
释放记忆
释放记忆
*/
q、 1>为什么最初多次调用复制构造函数
调用display(line)
,display(line2)
void display(line obj)
被定义为作为参数line
对象进行接收,因此每次调用display
时,都会为该函数复制该对象(就像向函数发送整数一样,它的值会被复制,这样如果函数修改了该整数,它只会影响函数的范围)。为了减少复制构造函数的数量,可以将display
定义为void display(Line&obj)
。但是请注意,对obj
所做的每一次修改都将反映在main()
中
q、 2>4次“释放记忆”和这两者之间的一次,我真的
困惑,你能帮我吗
两次(一次用于line
,另一次用于line2
),两次用于复制到display
函数的对象。执行display
流后,它将销毁所有本地对象,函数参数被视为本地对象
// Instead of calling display, this happens instead:
{ // Entering a new scope
Line obj = line1;
cout << "Length of line : " << obj.getLength() << endl;
} // Exiting scope
{ // Entering a new scope
Line obj = line2;
cout << "Length of line : " << obj.getLength() << endl;
} // Exiting scope