placement new to defer to a different constructor

前端 未结 7 982
滥情空心
滥情空心 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:40

    It looks like the best solution to this problem is to just create a different function to do the initialization:

    class Foo
    {
        inline void nullify()
        {
            // initialize things
        }
    
        Foo()
        {
            nullify();
        }
    
        Foo( int )
        {
            nullify();
        }
    }
    

提交回复
热议问题