简体   繁体   English

C ++,使用它从自身调用构造函数

[英]C++, using this to call a constructor from itself

I was writing some java last night and I had two constructors that looked basically alike, except my default constructor provided some values to my object, it was something like this: 昨晚我正在编写一些Java,并且我有两个看起来基本相同的构造函数,除了我的默认构造函数为对象提供了一些值外,它是这样的:

testObject(){
     width=5;
     height=12;
     depth=7;
     //other stuff is the same as the next one
}

testObject(int x, int y, int z){
    width=x; 
    height = y;
    depth = z;
    //All the other stuff is the same as default
}

So in this case, I was able to convert the code to do this instead: 因此,在这种情况下,我能够转换代码来执行此操作:

testObject(){
    this(5,12,7);
}

That sent the values from the default constructor back to the constructor as the 3-int constructor to be built as such. 这会将值从默认构造函数发送回构造函数,作为要像这样构建的3-int构造函数。 Is there any way to get this type of functionality in C++? 有没有办法在C ++中获得这种功能?

In C++0x, you can do this: 在C ++ 0x中,您可以执行以下操作:

TestObject() :TestObject{5, 12, 7} {}

See Delegating Constructors for more details. 有关更多详细信息,请参见委托构造函数 (Curly braces not look familiar to you? They're for preventing narrowing .) (弯括号对您来说似乎并不熟悉?它们用于防止变窄 。)

If you don't have C++0x available yet, then in your case, you can use default arguments as mentioned in other answers here. 如果您还没有C ++ 0x,那么根据您的情况,可以使用此处其他答案中提到的默认参数。

You were close, try calling the 3-parameter constructor in your initializer list for your default constructor. 您接近了,请尝试在初始值设定项列表中为默认构造函数调用3参数构造函数。

testObject(int x, int y, int z) :
    width(x), 
    height(y),
    depth(z) {
    //All the other stuff is the same as default
}

testObject() : testObject(5,12,7) {
     //other stuff is the same as the next one
}

There is no way to do this, but you can create a private method with initialization code and call it from your different constructors. 无法执行此操作,但是您可以使用初始化代码创建一个私有方法,然后从不同的构造函数中调用它。

EDIT: duplicate of Can I call a constructor from another constructor (do constructor chaining) in C++? 编辑: 我可以在C ++中从另一个构造函数(做构造函数链接)调用一个构造函数吗?

您可以在构造函数中使用默认值(即testObject(int x = 5,int y = 12,int z = 7))来执行所需的操作。

The most interesting thing is that you don't need to do that. 最有趣的是,您不需要这样做。 Default arguments are aimed at solving problems like this. 默认参数旨在解决此类问题。 Here is an example: 这是一个例子:

#include <iostream>

struct Foo
{
    int x, y, z;

    Foo (int x = 5, int y = 12, int z = 7)
        : x (x), y (y), z (z)
    {}
};

int
main ()
{
    Foo f1, f2 (1, 2, 3);
    std::cout << f1.x << ", " << f1.y << ", " << f1.z << '\n'
         << f2.x << ", " << f2.y << ", " << f2.z << '\n';
}

Just for example sake, you can call a constructor of a class using placement new, like this: 例如,您可以使用placement new调用类的构造函数,如下所示:

struct Foo
{
    int x, y, z;

    Foo (int x, int y, int z)
        : x (x), y (y), z (z)
    {}

    Foo ()
    {
        this->~Foo ();
        new (this) Foo (5, 12, 7);
    }
};

This feature also comes out of the box in C++11, you can read more here . C ++ 11中也提供了该功能,您可以在此处阅读更多内容。

You can add default variable values to your class. 您可以将默认变量值添加到类中。

testObject::testObject(int x, int y, int z = 5)

Then you can call it with testObject(1,2) and z will retrieve the default value of 5 然后您可以使用testObject(1,2)调用它,并且z将检索默认值5

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

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