Reputation: 158
I have a quick question in gcc 4.8 with the flag -std=c++11 enabled. 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.
Upvotes: 1
Views: 115
Reputation: 8824
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.
Upvotes: 1
Reputation: 476990
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.
Upvotes: 1