MSVC direct constructor call extension

前端 未结 1 953
迷失自我
迷失自我 2021-02-15 10:42

In this response, tloveless pointed out that it\'s possible in MSVC to use this->foo::foo(42); for constructor delegation to directly call a con

相关标签:
1条回答
  • 2021-02-15 11:21

    It is not constructor delegating. Try following code:

    #include <iostream>
    
    class C{
    public:
        C() { std::cout << "C" << std::endl; }
        ~C() { std::cout << "~C" << std::endl; }
    };
    
    struct foo
    {
        int m;
        C c;
        foo(int p) : m(p) { std::cout << "foo("<<p<<")\n"; }
        foo()
            : m(0)
        {
            this->foo::foo(42);
            std::cout << "foo(), " << m << "\n";
        }
    };
    
    int main()
    {
        foo f;
    }
    

    According to output field "c" is initialized twice but destroyed only once. As zneak noted, It is similar to new (this) foo(42).

    0 讨论(0)
提交回复
热议问题