Skip to main content
add note on if there was a child object which allocated memory
Source Link
lyngvi
  • 1.4k
  • 12
  • 20

If you want to be evil, you can use the in-place "new" operator:

class Foo() { Foo() { /* default constructor deliciousness */ } Foo(Bar myParam) { new (this) Foo(); /* bar your param all night long */ } }; 

Seems to work for me.

edit

As @ElvedinHamzagic points out, if Foo contained an object which allocated memory, that object might not be freed. This complicates things further.

A more general example:

class Foo() { private: std::vector<int> Stuff; public: Foo() : Stuff(42) { /* default constructor deliciousness */ } Foo(Bar myParam) { this->~Foo(); new (this) Foo(); /* bar your param all night long */ } }; 

Looks a bit less elegant, for sure. @JohnIdol's solution is much better.

If you want to be evil, you can use the in-place "new" operator:

class Foo() { Foo() { /* default constructor deliciousness */ } Foo(Bar myParam) { new (this) Foo(); /* bar your param all night long */ } }; 

Seems to work for me.

If you want to be evil, you can use the in-place "new" operator:

class Foo() { Foo() { /* default constructor deliciousness */ } Foo(Bar myParam) { new (this) Foo(); /* bar your param all night long */ } }; 

Seems to work for me.

edit

As @ElvedinHamzagic points out, if Foo contained an object which allocated memory, that object might not be freed. This complicates things further.

A more general example:

class Foo() { private: std::vector<int> Stuff; public: Foo() : Stuff(42) { /* default constructor deliciousness */ } Foo(Bar myParam) { this->~Foo(); new (this) Foo(); /* bar your param all night long */ } }; 

Looks a bit less elegant, for sure. @JohnIdol's solution is much better.

Source Link
lyngvi
  • 1.4k
  • 12
  • 20

If you want to be evil, you can use the in-place "new" operator:

class Foo() { Foo() { /* default constructor deliciousness */ } Foo(Bar myParam) { new (this) Foo(); /* bar your param all night long */ } }; 

Seems to work for me.