How to forward overloaded constructor call to another constructor in C++/CLI

独自空忆成欢 提交于 2019-11-28 07:39:17

问题


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() {}
}

回答1:


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.




回答2:


You can do following

ref class A
{
public:
    A(int p) : p(p) { this->A::A(); }
    A() : p(1) {}

    int p;
};

It is not valid C++ code , but VC compiles it fine :)




回答3:


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:

class A
{
private:
    void Inidialise() { /* common initialisation here */ }

public:
    A()       { Initialise(); /* specific initialisation for A() here */ }
    A(bool a) { Initialise(); /* specific initialisation for A(bool) here */ }
    A(int b)  { Initialise(); /* specific initialisation for A(int) here */ }

    /* etcetera */

}

It does not solve all 'problems' and it does not prevent all cases of duplicate code but it goes a long way.




回答4:


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.

class A 
{ 

public:

   A(int p) : p(p) 
   { new(this)A(); }

   A() : p(1) {}      

   int p; 
};


来源:https://stackoverflow.com/questions/2357076/how-to-forward-overloaded-constructor-call-to-another-constructor-in-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!