473,402 Members | 2,055 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,402 software developers and data experts.

Returned objects life-time

What is returned objects life-time?
For example, is it safe:

#include <...>

std::string foo()
{
return std::string("foo");
}

std::string bar()
{
std::string bar("bar");
return bar;
}

void test(char* a, char* b)
{
....
}

int main()
{
test(foo().c_str(), bar().c_str());
}

Here I think might be a problem. foo() returns std::string objects,
c_str() returns some pointer into the std::string("foo") and passes
this pointer to the test(). But will std::string("foo") exist when
test() is executed?

Oct 16 '06 #1
1 1498
Raider wrote:
What is returned objects life-time?
Since it's a temporary, refer to "the life time of a temporary"
in your favourite C++ book. Hint: any temporary *usually* lives
until the full expression where it's created is evaluated, except
when it lives longer if bound to a reference to const.
For example, is it safe:

#include <...>

std::string foo()
{
return std::string("foo");
}

std::string bar()
{
std::string bar("bar");
return bar;
}

void test(char* a, char* b)
{
...
}

int main()
{
test(foo().c_str(), bar().c_str());
That shouldn't compile. Your 'test' needs pointers to non-const char
and 'c_str' returns a pointer to const char.
}

Here I think might be a problem. foo() returns std::string objects,
c_str() returns some pointer into the std::string("foo") and passes
this pointer to the test(). But will std::string("foo") exist when
test() is executed?
From the lifetime point of view, yes. You still need to make your
code const-correct.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 16 '06 #2

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

Similar topics

22
by: Claudio Jolowicz | last post by:
Is it possible to store unique objects in an STL container? Suppose an object of class C is unique: class C { public: C() {} ~C() {} private:
1
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. We programmers can better understand program than english (: so... class A { };...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
6
by: zeroWePutTheSpamIntoTheBintype | last post by:
I'm having trouble using Braid (my user-defined type) objects returned from functions. I've got: Braid descend_n(int n, int no_strings) This function needs to use void left_multiply(Braid&b)....
5
by: Ian Meakin | last post by:
I am trying to consume a web service i have written. The web service returns to the client an array of objects. The web service when tested via inetrnet explorer retruns the correct xml data. I...
3
by: Rob | last post by:
Rather new to vb.net... I am wondering if the following is possible and sounds reasonable... Can connection objects and datasets be defined once (at program load) and be available for the life...
21
by: Andrew Ward | last post by:
Consider the following: #include <iostream> struct X { int x; X() { x = 6; } };
3
by: Divick | last post by:
I was reading this section in Bruce Eckel's book which talks about passing and returning large objects ( Chapter 11: References & the Copy-Constructor ), where he explains that how to return big...
2
by: oh.i.love.spam | last post by:
This is just an example... I don't have the access/ability to upload the actual code. Here is the meat in the XML that is returned from the $client- <VesselConfigurations...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.