复制构造函数被触发但是如何触发?
copy constructor is triggered but how?
这是我的代码:
#include <iostream>
#include <cstring>
using namespace std;
class someClass{
char * data;
public:
someClass(const char * s){
data = new char[strlen(s)+1];
strcpy(data,s);
cout << "constructing.."<<endl;
}
someClass(const someClass &o){
data = new char[strlen(o.data)];
strcpy(data,o.data);
cout << "copy construction" << endl;
}
~someClass(){delete [] data; cout << "destructing..." << endl;}
char * getValue(){return data;}
};
void showValue(someClass o){
char * s;
s = o.getValue();
cout << s << endl;
}
int main(int argc, char ** argv){
someClass str("Hello"), ptr("world");
showValue(str);
showValue(ptr);
}
输出为:
constructing..
constructing..
copy construction
Hello
destructing...
copy construction
world
destructing...
destructing...
destructing...
现在,只要我们在第 1 行的 main() 中创建对象,就会触发前两个 'constructing..'。
showValue(str) 运行s 并触发单词 'hello' 的复制构造函数。 如何?创建临时对象后,当它失效时会自行销毁。
showValue(ptr) 运行s 并触发单词 'world' 的复制构造函数。 如何?创建临时对象后,当它失效时会自行销毁。
最后,我们的 str 和 ptr 对象以相反的顺序被销毁。
为什么有copy ctor 运行?我没有将 someClass 对象发送到 someClass 对象。你能解释一下情况吗?
您打电话时正在复印
void showValue(someClass o)
如果要避免复制,请按引用传递
void showValue(someClass const& o)
在第一种情况下,o
是您的 someClass
参数的函数本地副本,它在函数结束时被销毁。
void showValue(someClass o){
char * s;
s = o.getValue();
cout << s << endl;
}
你cade的这一部分调用了someClass
的复制构造函数。
如果您想摆脱它,可以将 class 更改为:
void showValue(const someClass & o){
char * s;
s = o.getValue();
cout << s << endl;
}
并将 GetValue 更改为:
char * getValue() const {return data;}
这称为按常量引用传递,而不是按值传递(您正在做的)。当您按值传递时,该函数调用被调用的 class 的复制构造函数。
建议对getValue()
的更改是因为编译器希望确保showValue
内部对getValue
的调用不会修改const元素的成员对象const someClass & o
。
这是我的代码:
#include <iostream>
#include <cstring>
using namespace std;
class someClass{
char * data;
public:
someClass(const char * s){
data = new char[strlen(s)+1];
strcpy(data,s);
cout << "constructing.."<<endl;
}
someClass(const someClass &o){
data = new char[strlen(o.data)];
strcpy(data,o.data);
cout << "copy construction" << endl;
}
~someClass(){delete [] data; cout << "destructing..." << endl;}
char * getValue(){return data;}
};
void showValue(someClass o){
char * s;
s = o.getValue();
cout << s << endl;
}
int main(int argc, char ** argv){
someClass str("Hello"), ptr("world");
showValue(str);
showValue(ptr);
}
输出为:
constructing..
constructing..
copy construction
Hello
destructing...
copy construction
world
destructing...
destructing...
destructing...
现在,只要我们在第 1 行的 main() 中创建对象,就会触发前两个 'constructing..'。
showValue(str) 运行s 并触发单词 'hello' 的复制构造函数。 如何?创建临时对象后,当它失效时会自行销毁。
showValue(ptr) 运行s 并触发单词 'world' 的复制构造函数。 如何?创建临时对象后,当它失效时会自行销毁。
最后,我们的 str 和 ptr 对象以相反的顺序被销毁。
为什么有copy ctor 运行?我没有将 someClass 对象发送到 someClass 对象。你能解释一下情况吗?
您打电话时正在复印
void showValue(someClass o)
如果要避免复制,请按引用传递
void showValue(someClass const& o)
在第一种情况下,o
是您的 someClass
参数的函数本地副本,它在函数结束时被销毁。
void showValue(someClass o){
char * s;
s = o.getValue();
cout << s << endl;
}
你cade的这一部分调用了someClass
的复制构造函数。
如果您想摆脱它,可以将 class 更改为:
void showValue(const someClass & o){
char * s;
s = o.getValue();
cout << s << endl;
}
并将 GetValue 更改为:
char * getValue() const {return data;}
这称为按常量引用传递,而不是按值传递(您正在做的)。当您按值传递时,该函数调用被调用的 class 的复制构造函数。
建议对getValue()
的更改是因为编译器希望确保showValue
内部对getValue
的调用不会修改const元素的成员对象const someClass & o
。