473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question regarding Const Reference and Temporaries

Hi all,

Consider the following code fragment:

// some data structure
class Data { ... }

// Container for the data structure
Class Container
{
public:
Container(const& Data data): m_cRefData(data), m_ncRefData(data)
{}
private:
const Data& m_cRefData;
Data& m_ncRefData;
};

// some func to create the data
Data getData(int i, int j, string k, const char* l)
{
return Data(i,j,k,l);
}

// some func to process the container
void processContainer(Container& container)
{
// work with container.mcRefData and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l) );

// .... do some other things ... amount of time taken may varies ...

processContainer(container);
My question is what happen if I used the data member of the container?
Will the value initialized in the Container constructor still valid?
I read something about temporaries bind to const& have a lifetime of
the const&. If that holds, container.mncRefData may not be valid by
the time it is process but, container.mcRefData is still valid by the
time it is processed. Is that assumption correct?

Thanks,
Kaede
Jul 19 '05 #1
3 2265
kaede wrote:
Hi all,

Consider the following code fragment:

// some data structure
class Data { ... }

// Container for the data structure
Class Container
class, not Class.
{
public:
Container(const& Data data): m_cRefData(data), m_ncRefData(data)
{}
& is in the wrong place.

Container(const Data &data)

Also, I don't think you can initialize a non-const reference with a
const reference. I've never tried it or bothered to look it up in the
standard, but I'd be very surprised if it were legal.


private:
const Data& m_cRefData;
Data& m_ncRefData;
};

// some func to create the data
Data getData(int i, int j, string k, const char* l)
{
return Data(i,j,k,l);
}

// some func to process the container
void processContainer(Container& container)
{
// work with container.mcRefData and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l) );
The temporary returned from getData is destroyed after this expression
completes. Your references are no longer valid.

// .... do some other things ... amount of time taken may varies ...

processContainer(container);
My question is what happen if I used the data member of the container?
Undefined behavior.
Will the value initialized in the Container constructor still valid?
I read something about temporaries bind to const& have a lifetime of
the const&.


Not quite what happened here. The reference you bound the temporary to
was the parameter to the constructor. It died when the constructor finished.

In general, the compiler would have no way of knowing that you made
*another* reference to the temporary (if the construct were in a
different translation unit, for example), so it could not possibly
prolong the life of the temporary to match the life of the reference.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
WW
Kevin Goodsell wrote:
Container(const& Data data): m_cRefData(data),
m_ncRefData(data) {}
& is in the wrong place.

Container(const Data &data)


Or if we are at it:

Container(Data const &data)

;-)
Also, I don't think you can initialize a non-const reference with a
const reference. I've never tried it or bothered to look it up in the
standard, but I'd be very surprised if it were legal.


Only in The Sage mode. ;-) Can be activated by
the --allow-all-sort-of-silly-errors-and-swearing mode.

--
WW aka Attila
Jul 19 '05 #3
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<dX****************@newsread3.news.pas.earthl ink.net>...
Not quite what happened here. The reference you bound the temporary to
was the parameter to the constructor. It died when the constructor finished.

In general, the compiler would have no way of knowing that you made
*another* reference to the temporary (if the construct were in a
different translation unit, for example), so it could not possibly
prolong the life of the temporary to match the life of the reference.

-Kevin


Thanks, I think I understand the concept now. At first I thought, the
second reference that bound to the temporary would prolong the life of
the temporaries. I guess I made a wrong assumption there. I wrote
some code to try out the scenerio and it work just fine. I guess that
was just sheer luck.

Again Thanks for the advice.
Kaede
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kaede | last post by:
Hi all, I would like to know if the following code is valid and not ill-formed. Data getData() { return Data(1, 2, 3); }
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
5
by: Dave | last post by:
Hello all, I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that object were modified, what harm could...
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
6
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ??...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
7
by: Jess | last post by:
Hello, I learned that if we do "v.end()", then the returned iterator is a temporary object and hence cannot be changed like --v.end(); Why is the returned iterator a temporary pointer? I...
8
by: pauldepstein | last post by:
The following code was written by a colleague -- to preserve confidentiality, the name of the class is changed: HisClass operator+ (const HisClass & h1, const HisClass & h2) { // some code here}...
5
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.