Use of function fetch address and copy constructor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.