Solved

Initialize reference member with default value

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

747 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

9 Experts available now in Live!

Get 1:1 Help Now