473,320 Members | 1,865 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,320 software developers and data experts.

Return an object declared on stack

My understanding is that "MyString" is created within the function of "emit"
on stack. How could the value of "MyString" be returned to the caller
successfully?

string emit()
{
string MyString("This is a string from emit()");
return MyString;
}

int main()
{
string YourString;
YourString = emit();
...
}
Jul 19 '05 #1
2 3969
What you have is correct.

At the return of the emit function, a copy of MyString is sent back, and in
main the assignment operator is used to assign the value in the return value
to YourString.

Ali

"stub" <st**@asof.com> wrote in message
news:eu***********************@bgtnsc04-news.ops.worldnet.att.net...
My understanding is that "MyString" is created within the function of "emit" on stack. How could the value of "MyString" be returned to the caller
successfully?

string emit()
{
string MyString("This is a string from emit()");
return MyString;
}

int main()
{
string YourString;
YourString = emit();
...
}

Jul 19 '05 #2
On Mon, 03 Nov 2003 17:44:42 GMT, "stub" <st**@asof.com> wrote:
My understanding is that "MyString" is created within the function of "emit"
on stack. How could the value of "MyString" be returned to the caller
successfully?

string emit()
{
string MyString("This is a string from emit()");
return MyString;
}

int main()
{
string YourString;
YourString = emit();
...
}


There will be a temporary string object created from MyString which is
then returned. It is only bad to return a reference or pointer to an
object which is created on the stack because it goes out of scope when
the function returns. The temporary will last until its value is used
by the object to which it is assigned.
--
Bob Hairgrove
No**********@Home.com
Jul 19 '05 #3

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

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
3
by: Cong Wang | last post by:
Hi,all! I found an interesting problem,it is that how to implement a C function which can be called once and return twice? Just like the POSIX function fork() or the library function...
8
by: Lee Jackson | last post by:
Whats the difference between the following in terms of how the CLR allocates and garbage collects? Which is better in terms of memory usage/performance? a) foreach (string strURL in...
2
by: Arne | last post by:
How do I return a references to an object from a function? static ArrayList getItemFile(string destdir){...} Does the above code return the whole Arraylist on the stack or does it return a...
31
by: CeZaR | last post by:
Hi, How can i specify the return type of a function returning a managed array of chars. If i try to write: "char __gc func()" i get an error! How can i do that? Thanks!
3
by: Bob Altman | last post by:
Hi all, I have a basic question regarding holding references to objects in unmanaged C++. Suppose my unmanaged C++ class has a method that accepts a reference to an object as an argument, and...
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
7
by: Christian Christmann | last post by:
Hi, in my source code I've included a library I've not written and source code is available. In a function, I've generate an object of that library class dynamically with "new" and at the...
5
by: rishi.shah | last post by:
I wanted to know how the compiler creates and allocates memory for an object in C++. When I say Foo obj where Foo is a class does the memory for the attributes of class Foo get allocated on the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.