Thinking about constructors and this call

Source: Internet
Author: User

A series of thoughts and content raised from the following questions:
I need to invoke the constructor of another object in the constructor of one class and use this to initialize one of the reference members.

The main problems encountered:

1. Can you access this in the initialization list of the constructor?

It is obvious that C + + creates an object in two parts, creating memory and calling constructors.
Obviously in the initialization list, the memory occupied by the current object has been created, and Ok,this is accessible, except that some of the members are not initialized (because no constructor has been executed yet, only the object is partially valid).

That also indirectly illustrates the problem that the use of this in the function body of the constructor is completely reliable, and all the members of the compiler can initialize automatically (such as members of the base class, members with default constructors, and note that the specific initialization order is determined by the class Member definition order). However, the values of the current class that are not initialized in the initialization list and have no default constructor class member variables (such as common C + + built-in types, int, float, pointer, and so on) are undefined.

2. Constructor parameters have the same name as members

The constructor is as follows: with a parameter with the same name as the class member. The printout is only intended to verify that the member variable is initialized.

class a{public:    A (int a): A (a) {cout<<a<<Endl;} Private :     int A;};

Because this is not directly present in the initialization list, the compiler handles the same duplicate name. It is also said that you cannot explicitly use this as a qualifier in the initialization list of constructors, such as the following code cannot be compiled:

A:this->a (a) {}
3. How to call another constructor in a class's constructor sink

Constructors do not allow nested calls, but can call different overloaded forms. such as the following code: (Note This is an interview topic)

struct cls{    int  m_i;     int i): m_i (i) {}    cls () {CLS (0);}}; int Main () {    CLS obj;     << obj.m_i << Endl;     return 0 ;}

What is the output?

---------------------------------------------------------------------------

The answer is unknown because m_i is an uninitialized variable and is a wild value.

"CLS (0);" Statement that creates a temporary CLS object and initializes the member m_i of the object to 0. The value of the current object is not initialized.

If you need to implement a constructor class to call another constructor, you need to use the placement new operator. The code is as follows:

struct cls{    int  m_i;     int i): m_i (i) {}    cls ()    {        new (this) CLS (0);    }};    

If you don't know placement new, it's recommended to look at C + + primer or tcpl.

The above functionality can be implemented directly in c++11 by means of a delegate or inheritance constructor.

struct cls{    int  m_i;     int i): m_i (i) {}    CLS (): CLS (0) {}};

4. Solution

Write it down here. I have a basic understanding of the case where this is referenced in the initialization list of constructors, and you can use the following code to solve the problem raised in this article.

 class   Context;  class   ref{ public  : Ref (context  &context): Context (context) {  private  : Context  &context;};  class   context{ public  : Context ():  ref  (*this   private  : Ref  ref  ;};  

Retell the starting question: I need to invoke the constructor of another object in the constructor of one class, and use this to initialize one of the reference members.

The class context invokes the constructor of ref through this in its constructor's initialization list.

5. References

[1] talking about constructor call constructor http://www.cnblogs.com/chio/archive/2007/10/20/931043.html in C + + from a topic

[2] C + + one constructor calls another constructor http://www.cnblogs.com/ayanmw/archive/2012/08/20/2647808.html

Thinking about constructors and this call

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.