I know that there is no way to do this in pure C++, but I was wondering if it is possible to call a constructor from another constructor's initialization list in C++/CLI, same way one can do it in C#.
Example:
ref class Foo {
Foo() {}
Foo(int i) : Foo() {}
}
Just stumbled by, for the same question. In my case I'm using VS2010.
It is clear that VS2010 will never get updated to fully implement C++11, use VS2015 if you need better compliance with the standard (which I do when I can). But for some (legacy) projects I still have to use VS2010.
An approach that works in many cases (for me) is the use of a private function with all shared initialisation code in it. Example:
It does not solve all 'problems' and it does not prevent all cases of duplicate code but it goes a long way.
You can do following
It is not valid C++ code , but VC compiles it fine :)
When you said "I know that there is no way to do this in pure C++" you were in error. It is possible to do that in native C++. You can use the placement new operator to do so.
It is called a "delegating constructor". It is not available in the language yet. But there's a formal proposal, you'll find it in annex F.3.1 of the language specification. Given Microsoft's stance towards C++/CLI, that is unlikely to see the light of day anytime soon.
UPDATE: delegating constructors did have a life beyond the proposal in that annex, they were added to the standard C++11 language specification. Microsoft has been working on getting the C++11 additions implemented. Delegating constructors finally made it for VS2013. And they also work in C++/CLI in that edition.