Solved

Initialize reference member with default value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 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 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.

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now