473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference of the return value.

Question about reference of the function return object.

class A
{
public:
std::string getString() const {
return str_;
}

private:
std:: string str_;
};

function_ref(const A& a){
...
const std::string& str = a.getString(); //getString ()
return 'std::string' not 'const std:string&',
cout << "Text is: " << str; //use str here will be OK
...
}

function_point(const A& a) {
...
const std::string* pStr = &a.getString(); //the return
object of 'std::string' will destruct here
cout << "Text is: " << *pStr; //so using '*pStr' here may
cause a crash
...
}

My question is if there had any problem with reference of the
function return object, because 'a.getString()' just return a temp
object.
After I debugged this in VS2003, I found that the temp object will
be destroyed at the end of this function.
'function_ref(const A& a)' will be running without any problem in
VS2003 but 'function_point(const A& a)' may cause error.

I want to ask if that reference of a temp return value delay the
destruction is just a property of MS VS2003 or C++ standard.

Jun 4 '07 #1
8 1775
ss******@gmail.com wrote:
My question is if there had any problem with reference of the
function return object, because 'a.getString()' just return a temp
object.
No. The return is a temporary rvalue. If you bind it to a const
reference it will live as long as the reference.

Pointers, however, have no inteligence about what they refer to.
The temporary rvalue from the return, when not bound to a reference,
is destroyed at the end of the full expression it is created in.
In this case, it's history as soon as the initialization of the
pointer takes place.
Jun 4 '07 #2
No. The return is a temporary rvalue. If you bind it to a const
reference it will live as long as the reference.

Pointers, however, have no inteligence about what they refer to.
The temporary rvalue from the return, when not bound to a reference,
is destroyed at the end of the full expression it is created in.
In this case, it's history as soon as the initialization of the
pointer takes place.
Thanks for you help.

You answers let me know a concept of lvalue and rvalue.

So const reference to a temporary rvalue can be using a concept of
the c++ standard, not the VS2003 property, and this is another
difference between the reference and the pointer.

Jun 4 '07 #3
Ron Natalie wrote:
No. The return is a temporary rvalue. If you bind it to a const
reference it will live as long as the reference.
Is this also true for const references as member variables? If I try
this, it compiles without warnings but causes a segmentation fault
(at least with gcc 3.3):

#include <iostream>
#include <string>

class A
{
const std::string& s;

public:
A(): s("test2") {}
void print() { std::cout << s << std::endl; }
};

int main()
{
A a;
a.print();
}
Jun 4 '07 #4

Juha Nieminen <no****@thanks.invalidwrote in message...
Ron Natalie wrote:
No. The return is a temporary rvalue. If you bind it to a const
reference it will live as long as the reference.

Is this also true for const references as member variables? If I try
this, it compiles without warnings but causes a segmentation fault
(at least with gcc 3.3):

#include <iostream>
#include <string>

class A{
const std::string& s;
public:
A(): s("test2") {}
void print() { std::cout << s << std::endl; }
};

int main(){
A a;
a.print();
}
Think about what you are initializing the reference to.

std::string &rs( "Hi There" );
// Error: could not convert `"Hi There"' to `std::string&'
class A{
std::string s;
const std::string &rs;
public:
A(): s( "test2" ), rs( s ){}
void print() { std::cout << rs << std::endl;}
};

int main(){
A a;
a.print();
}

--
Bob R
POVrookie
Jun 4 '07 #5

BobR wrote in message...
>
Juha Nieminen <no****@thanks.invalidwrote in message...
Ron Natalie wrote:
No. The return is a temporary rvalue. If you bind it to a const
reference it will live as long as the reference.
Is this also true for const references as member variables? If I try
this, it compiles without warnings but causes a segmentation fault
(at least with gcc 3.3):

#include <iostream>
#include <string>
class A{
const std::string& s;
public:
A(): s("test2") {}
void print() { std::cout << s << std::endl; }
};

int main(){
A a;
a.print();
}

Think about what you are initializing the reference to.

std::string &rs( "Hi There" );
// Error: could not convert `"Hi There"' to `std::string&'

class A{
std::string s;
const std::string &rs;
public:
A(): s( "test2" ), rs( s ){}
void print() { std::cout << rs << std::endl;}
};

int main(){
A a;
a.print();
}
Oooops, sorry Juha. I see what you were doing now.
I forgot Murphy's [net] Laws - Never post while on your *first* cup of
coffee!

Oh well, maybe my example will help some newbie.
--
Bob R
POVrookie
Jun 4 '07 #6
Juha Nieminen wrote:
Ron Natalie wrote:
>No. The return is a temporary rvalue. If you bind it to a const
reference it will live as long as the reference.

Is this also true for const references as member variables? If I try
this, it compiles without warnings but causes a segmentation fault
(at least with gcc 3.3):

No.

Easy test code attached.
#include <iostream>
template <int N>
struct X
{
X()
{
std::cout << "X<" << N << ">::X()\n";
}

~X()
{
std::cout << "X<" << N << ">::~X()\n";
}
};
struct A
{
const X<1& x;

A()
: x( X<1>() )
{
std::cout << "A::A()\n";
}

~A()
{
std::cout << "A::~A()\n";
}
};
int main()
{
const X<2& x2 = X<2>();
A a;
}
Jun 5 '07 #7
Gianni Mariani wrote:
Juha Nieminen wrote:
> Is this also true for const references as member variables?
No.
Why doesn't gcc issue at least a warning? It's assigning a temporary
to the reference. Can't it see that in this case it will cause misbehavior?
Jun 6 '07 #8
Juha Nieminen wrote:
Gianni Mariani wrote:
>Juha Nieminen wrote:
>> Is this also true for const references as member variables?
>No.

Why doesn't gcc issue at least a warning? It's assigning a temporary
to the reference. Can't it see that in this case it will cause misbehavior?
That's a good suggestion. I'd probably file a bug with GCC and see what
happens.
Jun 10 '07 #9

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

Similar topics

9
11890
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
5
3026
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
8
2378
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
29
3618
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
27
3091
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.