Solved

Initialize reference member with default value

Posted on 2008-10-29
3
348 Views
Last Modified: 2010-05-18
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++?

0
Comment
Question by:bachra04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22831469
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

0
 
LVL 2

Author Comment

by:bachra04
ID: 22832368
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,
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22832487
>> the problem is that I cannot use the operator =

Then you create the default value like this :

        boost::asio::io_service defaultService(the_needed_parameters);

There's no problem.


>> Also the when the client does that :
>> 
>> ProtocolInfo info;

That is already covered by the same constructor. Since the code I posted is using a default argument, it means that if the ProtocolInfo object is constructed by passing an argument, that argument will be used to initialize the reference. If no argument is passed to the constructor, the default value will be used.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question