C ++ Class Object return and reference instance explanation, object instance explanation

Source: Internet
Author: User

C ++ Class Object return and reference instance explanation, object instance explanation

I. Return of class objects

Mentioned in the copy constructor:

class A{};A func(A a){    return a;}int main(){    A x;    func(x);}

The transfer of parameters in calling func (a) is essentially a call to the copy constructor. The real parameter x is copied to the form parameter.

A func(A &a){    return a;}int main(){    A x;    func(x);}

This time, the func parameter is replaced with a reference. Obviously, this will not be a copy structure. a is just an alias of x. The references below will focus on this.

Here we will focus on rerturn a; how such a code returns is actually similar to the return of common variables. It also opens up a space for a temporary variable, which may be in registers, it is also possible to take the returned value of a to the space opened for temporary variables on the stack and return it to the main function, take out the value of the temporary variable and put it in the space of the new variable. The following uses a code example to complete the output:

class A {public:    A() {        cout << this << "--this is a constructor" << endl;    }    ~A() {        cout << this << "--this is a destructor" << endl;    }    A(const A & another) {        cout << this << "--copy cnnstructor from--" << & another << endl;    }    A & operator=(const A & other) {        cout << this << "--assignment operator overloading from--" << &other << endl;    }};A func(A a) {    return a;}int main() {    A x;    A t;    t = func(x);    return 0;}

0x28fead is the address of the x object, which is constructed once. 0x28feac is the address of the t object and is constructed once. 0x28feaf is the address of the form parameter a in func. It is obviously obtained by copying and constructing x, 0x28feae is the space address opened for the temporary variables that store the value of a. It is clear that the space address obtained by copying the value of a is constructed, and t is reloaded by the value assignment operator, take the values in the space where the temporary variables are stored to the space where t is located. We can see that the structure order is the opposite to the construction order.

Ii. Class Object Reference

If we change the above function to this, what will happen:

A  & func(A a) {    return a;}

We can see that there is no space available for temporary variables, but the value of the address where the parameter a is located is directly given to the space where the object t is located. Why?

This is the power of reference. Here it is equivalent to extending the scope of a to the main function, so that a can be directly assigned to t. Similar examples refer to the example above that does not have a copy structure:

A func(A &a){    return a;}int main(){    A x;    func(x);}

The same reason is that the scope of x is extended to the func function, where a is x, and x is a. Copy construction is not required.

That is to say, transferring a reference is equivalent to extending the scope.

If the code continues to be modified:

A & func() {    A a;    return a;}int main() {    A x;    A t ;    t = func();    return 0;}

In this case, let him return the reference, but then let's look at the result:

The space 0x28fe7f is released before the value assignment operator is overloaded. Because a is a local variable in func at this time, once the func scope exists, the structure is automatically executed. Even if the reference is returned, this address space is no longer, if multiple threads are being executed, other things may be put. The things assigned to t are unknown. Therefore, this is not allowed.

In this case, only the object itself can be returned, that is, the this pointer. Like the value assignment operator overload, its format is

Class Name & operator = (const Class Name & Object Name) {... return * this ;}

This pointer is returned in the function body.

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.