Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

asked on

Initialize reference member with default value

Hi This is what I want do in my class ProtocolInfo:
I have two contructors :
ProtocolInfo();
ProtocolInfo(boost::asio::io_service& pIo_service);

when my client calls ProtocolInfo, internally I creates a new ProtocolInfo object and assign to its internal member variable :
boost::asio::io_service&       m_pIo_service; a value which can only be passed in initialization list.

My question is how to creates the original ProtocolInfo with default value since a reference member cannot be initialized with NULL reference in c++?

Avatar of Infinity08
Infinity08
Flag of Belgium image

You have to create a default boost::asio::io_service object, and then you can simply use a default argument :
boost::asio::io_service defaultService;
 
class ProtocolInfo {
  private :
    boost::asio::io_service &m_pIo_service;
 
  public :
    ProtocolInfo(boost::asio::io_service &pIo_service = defaultService) : m_pIo_service(pIo_service) { }
};

Open in new window

Avatar of bachra04

ASKER

the problem is that I cannot use the operator = (boost::io_service is not copyable only through initalization list).
Also the when the client does that :

ProtocolInfo info;

here I want internally to call the constructor :

ProtocolInfo(),
:m_pIo_service(defaultService)...

but where in the code I declare it ?

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial