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

前端 未结 4 790
醉酒成梦
醉酒成梦 2020-12-20 17:50

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, s

相关标签:
4条回答
  • 2020-12-20 18:28

    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.

    0 讨论(0)
  • 2020-12-20 18:37

    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.

    0 讨论(0)
  • 2020-12-20 18:42

    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 :)

    0 讨论(0)
  • 2020-12-20 18:45

    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; 
    };
    
    0 讨论(0)
提交回复
热议问题