I visited the csdn forum today and found the following two sections of code: I have answered both questions, at the same time, I hope more people will benefit from posting to my blog.
The first section is the use of the replication constructor.
class c{string name;public:c(const string& s):name(s){}};void fn(c& s){cout<<"ok!"<<endl;}int main(){fn(string("Jenny"));}
// This Code cannot be compiled in vs2005, because vs can only export C from string, and it cannot automatically export C from string &. Remove the reference of function FN. ------ Answer: first, the string in the main function generates an unknown object, and then calls the copy constructor of C to generate an unknown C object, the second reason is that the reference must be a famous object and must be initialized before the reference operation can be performed in C ++.
The second step is to operate the function's local address retrieval.
# include <iostream>using namespace std;int func(){int a = 2;return a;}int main(void){cout << &func() << endl; //提示这句错误,需要左值,如果返回一个对象却可以return 0;}
# include <iostream>using namespace std;class human{public:void set(int i){x = i;}int get(){return x;}private:int x;};human func(){human a;a.set(123);cout << a.get()<<endl;return a;}int main(void){cout << &func() << endl; //可以取地址return 0;}
<span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245); ">//解答:一般的,如果返回值是内建类型的话,例如int,short,char之类的,通常将返回值保存在eax,ax,al寄存器里面,自然就无法取得地址了。</span>
Use of function fetch address and copy constructor