placement new to defer to a different constructor

前端 未结 7 978
滥情空心
滥情空心 2021-01-04 12:28

Is this safe? I\'m not using any virtual functions in my actual implementation, but I\'m tempted to believe that even if I was, it would still be safe.

clas         


        
7条回答
  •  太阳男子
    2021-01-04 12:44

    As others said, is a bad idea, and as a possible destructive case: what if you do

    class Foo
    {
        Foo()
        {
            // initialize things
        }
    
        Foo( int bar )
        {
             new ( this ) Foo(bar);
        }
    }
    

    welcome no the land of infinite recursion.

提交回复
热议问题