Is it possible to defer member initialization to the constructor body?

后端 未结 8 667
一生所求
一生所求 2021-01-13 14:25

I have a class with an object as a member which doesn\'t have a default constructor. I\'d like to initialize this member in the constructor, but it seems that in C++ I can\'

8条回答
  •  梦毁少年i
    2021-01-13 14:36

    I think your solution is the correct way to do things.

    You can also postpone the creation of the object by making is pointer (however it changes the code and data type):

    std::auto_ptr _sock;
    

    And then in body:

    _sock.reset(new udp::soket(_io_service, ep));
    

    But I think that your "workaround" is rather correct solution then workaround.

提交回复
热议问题