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

I have a class with an object as a member that does not have a default constructor. I would like to initialize this element in the constructor, but it seems that in C ++ I cannot do this. Here is the class:

#include <boost/asio.hpp>
#include <boost/array.hpp>

using boost::asio::ip::udp;

template<class T>
class udp_sock
{
    public:
        udp_sock(std::string host, unsigned short port);
    private:
        boost::asio::io_service _io_service;
        udp::socket _sock;
        boost::array<T,256> _buf;
};

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost",
  unsigned short port = 50000)
{
    udp::resolver res(_io_service);
    udp::resolver::query query(udp::v4(), host, "spec");
    udp::endpoint ep = *res.resolve(query);
    ep.port(port);
    _sock(_io_service, ep);
}

The compiler basically tells me that it cannot find the default constructor for udp :: socket, and from my research I realized that C ++ implicitly initializes each member before calling the constructor. Is there a way to do this the way I wanted to do it, or is it too "Java oriented" and not possible in C ++?

I worked on the problem, defining my constructor as follows:

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost",
  unsigned short port = 50000) : _sock(_io_service)
{
    udp::resolver res(_io_service);
    udp::resolver::query query(udp::v4(), host, "spec");
    udp::endpoint ep = *res.resolve(query);
    ep.port(port);
    _sock.bind(ep);
}

So my question is more out of curiosity and a better understanding of OOP in C ++

+3
7

, "" :

, ( ) ...

, :

class Example
{
public:
  Example();
private:
  Bar mAttr;
};

// You write
Example::Example() {}

// The compiler understands
Example::Example(): mAttr() {}

, , , .

. "" - :

class Example { public: Example(); private: Bar* mAttr; };

Boost.Optional :

class Example
{
public: Example();
private:
  Bar& accessAttr() { return *mAttr; }
  const Bar& getAttr() const { return *mAttr; }
  boost::Optional<Bar> mAttr;
};

Example::Example() { mAttr = Bar(42); }

Boost.Optional , ( ) .

+8
+1

++ , ,

, ctors, til ++ 0x (. )

0

:

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost", unsigned short port = 50000)
    :res(_io_service)
    ,query(udp::v4(), host, "spec")
    ,ep(*res.resolve(query))
    ,_sock(_io_service, ep)
{
}

: , 'res', 'query' 'ep' . ( _sock) :

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost", unsigned short port = 50000)
    :_sock(_io_service, udp::resolver(_io_service).resolve(udp::resolver::query(udp::v4(),host,"spec"))
{
}
0

, - -.

, ( ):

std::auto_ptr<udp::socket> _sock;

:

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

, " " - , .

0

_sock :

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/scoped_ptr.hpp>

using boost::asio::ip::udp;

template<class T>
class udp_sock
{
    public:
        udp_sock(std::string host, unsigned short port);
    private:
        boost::asio::io_service _io_service;
        boost::scoped_ptr<udp::socket> _sock_ptr;
        boost::array<T,256> _buf;
};

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost",
  unsigned short port = 50000)
{
    udp::resolver res(_io_service);
    udp::resolver::query query(udp::v4(), host, "spec");
    udp::endpoint ep = *res.resolve(query);
    ep.port(port);
    _sock_ptr.reset(new udp::socket(_io_service, ep));
}
0

ep:

#include <boost/asio.hpp>
#include <boost/array.hpp>

using boost::asio::ip::udp;

template<class T>
class udp_sock
{
    public:
        udp_sock(std::string host, unsigned short port);
    private:
        static udp::endpoint build_ep(const std::string &host,
          unsigned short port, boost::asio::io_service &io_service);

        boost::asio::io_service _io_service;
        udp::socket _sock;
        boost::array<T,256> _buf;
};

template<class T>
udp::endpoint udp_sock<T>::build_ep(const std::string &host,
  unsigned short port, boost::asio::io_service &io_service)
{
    udp::resolver res(io_service);
    udp::resolver::query query(udp::v4(), host, "spec");
    udp::endpoint ep = *res.resolve(query);
    ep.port(port);
    return ep;
}

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost",
  unsigned short port = 50000)
    : _sock(_io_service, build_ep(host, port, _io_service))
{
}
0

Source: https://habr.com/ru/post/1737386/


All Articles