C ++ one constructor calls another constructor

Source: Internet
Author: User

Due to the recently writtenCodeA little exception was found. Why is the value incorrect? Modified.

After debugging, we found that it was a problem in the constructor.

Search: C ++ one constructor calls another constructor

It turns out that this is a common problem. The Compiler also provides warnings. In fact, the syntax is completely reasonable. If it is Java, there should be no problem.

So how does C ++ call another constructor from one constructor?

Use the following method.

 

New(This) Constructor (different parameters );

 

Reference: http://www.cnblogs.com/chio/archive/2007/10/20/931043.html

I found the problem. It is wrong to return another constructor In the constructor because the constructor does not return the value.

However, your instance can be copied directly using the constructor. By default, the class has a = Operator.

The pointer can also be a pointer = new Constructor (); it's just a copy of the memory.

 

Is it necessary to delete the new operator and the previously searched new operator? Some of the answers are similar. This new is the operator.

Definition of the new expression (placement new) in the standard library

Inline void * _ cdecl operator new (size_t, void * _ p)

{

Return (_ P );

}

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 format is as follows:

 

 

Found

Http://forum.ubuntu.org.cn/viewtopic.php? T = 315229

You can use this-> to call other constructors...

After the experiment, it seems that it does not work either: the Error error c2273: "function style conversion": the error is displayed on the right of the "->" operator.

If you use the private function, you can... No.

 

 

 

The following sections of the discussion are also good:

Http://topic.csdn.net/u/20091219/18/4973528c-b6dd-4479-975d-54072cbcef9e.html


The following content is taken from
Http://zh.wikipedia.org/zh-cn/C%2B%2B0x#.E7.89.A9.E4.BB.B6.E5.BB.BA.E6.A7.8B.E7.9A.84.E6.94.B9.E8.89.AF

Object Construction Improvement
In standard C ++, a constructor cannot call other constructor. Each constructor must Initialize all members or call a shared member function. The constructor of the base class cannot be directly used as the constructor of the derived class. Even if the constructor of the base class is sufficient, each extended class must be constructed by itself. Non-constant data members in the class cannot be initialized at the declared place. They can only be initialized in the constructor.
C ++ 0x will provide solutions to these problems.

C ++ 0x allows constructors to call other constructors, which are called delegation or transfer (delegation ).
You only need to add a small amount of code to achieve function reuse between several constructors ). Java and C # both provide this function. The C ++ 0x syntax is as follows:

 Class  Sometype {  Int  Number;  String  Name; sometype (  Int I, String & S): Number (I), name (s ){}  Public  : Sometype (): sometype (  0 , " Invalid  "  ) {} Sometype (  Int I): sometype (I, "  Guest  "  ) {} Sometype (  String & S): sometype ( 1  , S) {postinit ();}}; 

In C ++ 03, the execution exit of the constructor indicates that the object is constructed. The C ++ 0x that allows the use of the constructor is constructed by means of a "any" constructor. With the constructor, the code in the function ontology will continue to run after the constructor completes (postinit () in the preceding example ()). If the base class uses a transfer constructor, the constructor of the derived class starts running only after the constructor of the "all" base class is complete.

C ++ 0x allows the derived class to manually inherit the construction of the base class,
The compiler can use the construction of the base class to construct the derived class. However, the actions that take the construction of the base class into the derived class cannot be carried in selectively,
Or inherit all the constructors of the base class, or do not inherit any of them (do not bring them manually ).
In addition, if multiple inheritance is involved, the constructors inherited from multiple base classes cannot have the same function signature ).
The newly added constructor of the derived class cannot have the same function signature as the inherited base constructor, because this is equivalent to repeated declaration.

Syntax:

 
ClassBaseclass {Public: Baseclass (IntIvalue );};ClassDerivedclass:PublicBaseclass {Public:UsingBaseclass: baseclass ;};

This syntax is equivalent to the Construction of derivedclass (INT) declared by derivedclass. In addition, because derivedclass has an inherited constructor, there is no default constructor.

On the other hand, C ++ 0x can use the following syntax to complete member initialization:

 
ClassSomeclass {Public: Someclass (){}ExplicitSomeclass (IntInewvalue): ivalue (inewvalue ){}Private:IntIvalue =5;};

If the initial value of ivalue is not set in the constructor, class members are initialized to set the initial value of ivalue to 5. In the above example, the constructor without parameters uses the default value, while the constructor with an integer parameter initializes the specified value.

In addition to the value assignment ("=") in the preceding example, you can also use the constructor and uniform initialization (Uniform ).
Initialization, use "{}").

Related Article

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.