即使没有要复制的对象,复制构造函数也会自动调用吗?
is copy constructor called automatically even when there isn't any object to be copied?
我在网上找到了这段代码:
#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 line(10);
display(line);
return 0;
}
执行这段代码的结果是:
Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
我不明白为什么在没有对象作为参数传递给复制构造函数时调用复制构造函数?
另外,在调试时,我了解到在函数 main 完成后调用了析构函数。为什么调用它以及为什么在函数 main 终止后调用它?
谢谢,
函数 display 按值获取它的参数,因此调用了复制构造函数。如果不需要,请通过引用传递它 - Line & obj。更好的是,通过 const 引用调用它 - const Line & obj。但在后一种情况下,您在 display 内部调用的成员函数也必须是 const。
void display(Line obj) {
此函数按值获取其参数。这意味着将此参数传递给此函数将复制它。当 main()
调用 display()
.
时,这是调用复制构造函数的地方
如果您更改此函数,使其通过引用获取其参数:
void display(Line &obj) {
您会发现您的示例程序不再调用复制构造函数。
您将在 C++ 书籍中找到有关按值传递参数与按引用传递参数的更多信息。
display()
中的参数:
void display(Line obj)
调用复制构造函数,因为在函数的参数中创建了一个新对象。因此,您传递给此函数的任何 class Line
对象都将被复制并用作函数体的参数。
您在以下行中调用此函数:
display(line);
因此,在 display()
中复制了 line
。为避免这种情况,请改为引用 class Line
的对象。将参数在函数头中传递的方式更改为:
void display(Line &obj)
这样,您只是在引用该对象,因此不会复制它。所以这里不会调用拷贝构造函数
我在网上找到了这段代码:
#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 line(10);
display(line);
return 0;
}
执行这段代码的结果是:
Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
我不明白为什么在没有对象作为参数传递给复制构造函数时调用复制构造函数? 另外,在调试时,我了解到在函数 main 完成后调用了析构函数。为什么调用它以及为什么在函数 main 终止后调用它? 谢谢,
函数 display 按值获取它的参数,因此调用了复制构造函数。如果不需要,请通过引用传递它 - Line & obj。更好的是,通过 const 引用调用它 - const Line & obj。但在后一种情况下,您在 display 内部调用的成员函数也必须是 const。
void display(Line obj) {
此函数按值获取其参数。这意味着将此参数传递给此函数将复制它。当 main()
调用 display()
.
如果您更改此函数,使其通过引用获取其参数:
void display(Line &obj) {
您会发现您的示例程序不再调用复制构造函数。
您将在 C++ 书籍中找到有关按值传递参数与按引用传递参数的更多信息。
display()
中的参数:
void display(Line obj)
调用复制构造函数,因为在函数的参数中创建了一个新对象。因此,您传递给此函数的任何 class Line
对象都将被复制并用作函数体的参数。
您在以下行中调用此函数:
display(line);
因此,在 display()
中复制了 line
。为避免这种情况,请改为引用 class Line
的对象。将参数在函数头中传递的方式更改为:
void display(Line &obj)
这样,您只是在引用该对象,因此不会复制它。所以这里不会调用拷贝构造函数