Constructor in C ++ calls Constructor

Source: Internet
Author: User

Recently, when I learned about C ++, I suddenly thought about how to call the constructor in C ++. The common error is to call the constructor as follows:

 1:  #include 
 3:  class Test
 4:  {
 5:  public:
 6:      int m_a;
 8:      Test(int a)
 9:      {
10:          m_a = a;
11:      }
13:      Test()
14:      {
15:          Test(1);
16:      }
17:  };
19:  int main(int argc,char* argv[])
20:  {
21:      Test var;
22:      std::cout<<var.m_a<<std::endl;
23:      return 0;
24:  }

This code output an uncertain value. The M_a value is not 1 because it is not used to initialize the current memory zone when test (1) is executed, instead, it initializes the memory zone of a temporary object.

So how can we call constructor in C ++?
Here, we need to describe another new expression-position new expression (placement new), which is used to initialize an object in the allocated original memory, unlike other new versions, it does not allocate memory.
The prototype in STL is as follows:

1:  void * operator new (size_t, const std::nothrow_t &) throw();
2:  void * operator new (size_t, void *) throw();
3:  void * operator new[] (size_t, const std::nothrow_t &) throw();
4:  void * operator new[] (size_t, void *) throw();

The expression is in the following format:

1:  new (place_address) type
2:  new (place_address) type (initializer-list)

Here, place_address must be a pointer, while intializer-list provides (possibly empty) initialization to use it when constructing newly allocated objects.

For the above example, we can use the new expression to call between constructors:

1:  Test()
2:  {
3:      new (this) Test(1);
4:  }

Finally, for the issue of mutual calling of constructor, consider the following two suggestions:
1) You can consider using the default parameters of the constructor to reduce this call method.
2) If you only reuse the code of another constructor for one constructor, you can extract the public part of the constructor and define a member function (private is recommended ), then you can call this function in every constructor that requires this code.

Appendix: Example of mutual calls of constructors in C #

 1:  public class Base
 2:  {
 3:      int _a;
 4:      int _b;
 5:  
 6:      public Base()
 7:      {
 8:          _b = 2;
 9:      }
10:  
11:      public Base(int a) :this()
12:      {
13:          _a = a;
14:      }
15:  }

Simply put, it is the this (Params) statement.

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.