简体   繁体   English

在同一个类的构造函数中调用构造函数

[英]Calling Constructor with in constructor in same class

I was expecting the output 2, 3 but I'm getting garbage value. 我期待输出2,3,但我得到了垃圾值。 Why's that? 为什么?

Here's my code: 这是我的代码:

#include <iostream>
using namespace std;

class A
{
public:
    int a, b;

    A()
    {
        cout << a << " " << b;
    }

    A(int x, int y)
    {
        a = x;
        b = y;
        A(); // calling the default constructor
    }
};

int main()
{
    A ob(2, 3);
    return 0;
}

Inside this constructor: 在这个构造函数中:

A(int x, int y)
{
    a = x;
    b = y;
    A(); // calling the default constructor
}

call A(); A(); creates a new temporary object that is immediately deleted after this statement. 创建一个在此语句后立即删除的新临时对象。 Because the default constructor A() does not initializes data members a and b then it outputs a garbage. 因为默认构造函数A()没有初始化数据成员ab然后它输出一个垃圾。

This temporary object has nothing common with the object created by constructor A( int, int ) . 此临时对象与构造函数A( int, int )创建的对象没有任何共同之处。

You could rewrite your class the following way: 您可以通过以下方式重写您的课程:

class A
{
public:
    int a, b;

    A(): A(0, 0) {}

    A(int x, int y) : a(x), b(y)
    {
        cout << a << " " << b;
    }
};

Here you output a and b which are not initialized: 在这里输出未初始化的a和b:

A(){
cout<<a<<" "<<b;
}

And here you initialize a and b, but you create an anonlymous temporary object 在这里你初始化a和b,但是你创建了一个非常有用的临时对象

A(int x , int y){
    a = x; b= y;
    A(); // !!!! CREATES AT TEMPORARY ANONYMOUS OBJECT WITH IT'S OWN a and B
}

To use a delegated constructor (ie using another constructor to finish the construction process of the SAME object) you have to use the delegeted constructor in the initialisation list : 要使用委托构造函数(即使用另一个构造函数来完成SAME对象的构造过程),必须在初始化列表中使用delegeted构造函数:

A(int x , int y) : A() { a=x; b=y;  } 

Unfortunately , when you use delegation, the delegate must be the ONLY meminitializer in the list. 不幸的是 ,当您使用委托时,委托必须是列表中唯一的meminitializer。 This requires that the initialisation of a and b would happen after A(). 这要求a和b的初始化将在A()之后发生。

Another alternative: 另一种选择:

class A
{
public:
    int a, b;
    A() :  A(0, 0) { }  // use a delegated ctor 
    A(int x, int y) : a(x), b(y) { cout << a << " " << b; }
};

Still another alternative, using defaults (without reusing a constructor) : 还有另一种选择,使用默认值(不重用构造函数):

class A
{
public:
    int a, b;
    A(int x=0, int y=0) : a(x), b(y) { cout << a << " " << b; }
};

You didn't call the default constructor, what you did is create a temporary A object, which happen to have its members uninitialized before you printed the output. 您没有调用默认构造函数,您所做的是创建一个临时A对象,它恰好在打印输出之前使其成员未初始化。 Perhaps what you wanted to do is constructor delegation, which would look like 也许你想要做的是构造函数委托,它看起来像

#include <iostream>

class A
{
    int a, b;
public:
    A(): A( 0, 0 ) {
        std::cout << a << ", " << b << std::endl;
    }
    A( int x, int y ): a( x ), b( y ) {}
}

int main()
{
    A object_a {}; //prints 0, 0
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM