简体   繁体   English

C ++ 11类中的成员初始化与此

[英]c++11 in-class member initialization with this

I have a quick question in gcc 4.8 with the flag -std=c++11 enabled. 我在启用了-std = c ++ 11标志的gcc 4.8中有一个快速问题。 I can do this and it works fine. 我可以做到,而且效果很好。

class Test;
class StupidClass {

public:
    StupidClass(Test *test) {}
};

class Test {
    StupidClass c = StupidClass(/*this is the part in question*/ this);
};

and i wanted to know if this is valid c++11 having "this" in an in-class member initialization like this. 我想知道这是否是有效的c ++ 11,在这样的类内成员初始化中具有“ this”。

If you write 如果你写

struct Foo
{
    Bar bar { this };
};

that is no different from: 区别于:

struct Foo
{
    Foo() : bar(this) { }
    Bar bar;
};

So if the second one makes sense, so does the first. 因此,如果第二个有意义,则第一个也有意义。

it is valid but you need to be careful as this is not yet fully valid. 它是有效的,但是您需要小心,因为这还没有完全有效。 Storing a pointer or reference is fine, using a member declared before the one receiving this is fine too. 存储指针或引用是可以的,可以使用在接收该指针或引用之前声明的成员也可以。

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

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