Solved

Initialize reference member with default value

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

Suggested Solutions

Title # Comments Views Activity
Header of docx file 17 132
Why isn't object file created? 6 93
Error creating a new C++ project in ,net 20 51
Outlook 13 84
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

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