C ++ overloaded constructors cannot call each other.

Source: Internet
Author: User

C ++ overloaded constructors cannot call each other.

The overloaded constructors in the java class can call each other, as shown in the following code:

 1 public class TestConstructor { 2     private int value; 3  4     public TestConstructor(int value) { 5         this.value = value; 6         System.out.println("constructor1:"+this); 7     } 8  9     public TestConstructor() {10         this(10);11         System.out.println("constructor2:"+this);12     }13 14     public static void main(String[] args) {15         TestConstructor test = new TestConstructor();16         System.out.println(test.value);17         System.out.println(test);18     }19 }

The code execution result is:

Constructor1: TestConstructor @ 74a14482
Constructor2: TestConstructor @ 74a14482
10
TestConstructor @ 74a14482

It can be seen that the result is expected. The value assignment is successful and only one object is created.

 

 

Let's take a look at the C ++ implementation (header file omitted ):

 1 #include "testconstructor.h" 2 #include <QDebug> 3  4 TestConstructor::TestConstructor() 5 { 6 //    this(10); 7     TestConstructor(10); 8     qDebug()<<"constructor1:"<<this; 9 }10 11 TestConstructor::TestConstructor(int value)12 {13     this->value = value;14     qDebug()<<"constructor2:"<<this;15 }
 1 #include "testconstructor.h" 2 #include <QDebug> 3  4 int main(int argc, char *argv[]) 5 { 6     TestConstructor *t = new TestConstructor(); 7     qDebug()<<t->value; 8     qDebug()<<t; 9     delete t;10 }

The code execution result is:

Constructor2: 0x22fcf0

Constructor1: 0xdadfb0

15574896

0xdadfb0

On the one hand, the value set for value does not take effect. On the other hand, two constructors create two different objects, which means C ++ cannot call each other like java.

Solution:

Most constructors need to call each other with default parameters. In the C ++ function declaration, you can directly set default parameters (java does not support default parameters ), in this way, you do not need to overload the constructor:

TestConstructor(int value = 10);

  

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.