Solved

Initialize reference member with default value

Posted on 2008-10-29
3
334 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

837 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