Simple C++ question: Using return values (by value) 
Author Message
 Simple C++ question: Using return values (by value)

Quote:

>Hi!

>I have a member function like:

>string LgInfo::GetInfo() {
>    string s("");

Just say:

 string s;

which might be more efficient than initializing a string object with a
zero-length string. Ignoring memory allocation differences, it means the
same thing, it looks cleaner, and you avoid creating an unnecessary string
literal.

Quote:
>   //...
>    return s;
>}

>Then I use lots of client code, that converts the return value
>in a c-string in order to assign to a MFC CString, like:

>    LgInfo info;
>    CString cS = info.GetInfo().c_str();   // using ".foo()" safe???

>Now, I am suddenly unsure about using the return value
>as an object and append this ".c_str()".

>Is this safe???
>Or does it work just in debug mode???

The temporary string returned by GetInfo() won't be destroyed until the end
of the full-expression it appears in, which here means GetInfo().c_str() is
good "until the semicolon". In particular, it's valid for the CString ctor,
and because CString makes a copy of the char array, you're fine. Just don't
do this:

 const char* p = info.GetInfo().c_str();
 // The temporary string returned from GetInfo() has been destroyed
 // here, and p points to garbage.

Quote:
>Wyh am suddenly unsure is, because I now, when my
>function would return a _Reference_ (&), then I _know_
>I can say info.GetInfo().c_str().

>But the function returns an object by value!?!?

It may not be safe when you return by reference. For example, don't do this:

string& LgInfo::GetInfo() {
    string s;
    return s; // Bad!

Quote:
}

That returns a reference to a destroyed object. Returning by value is what
saved you here!

--
Doug Harrison

Visual C++ MVP



Sun, 21 Apr 2002 03:00:00 GMT  
 Simple C++ question: Using return values (by value)
Hi!

I have a member function like:

string LgInfo::GetInfo() {
    string s("");
   //...
    return s;

Quote:
}

Then I use lots of client code, that converts the return value
in a c-string in order to assign to a MFC CString, like:

    LgInfo info;
    CString cS = info.GetInfo().c_str();   // using ".foo()" safe???

Now, I am suddenly unsure about using the return value
as an object and append this ".c_str()".

Is this safe???
Or does it work just in debug mode???

Wyh am suddenly unsure is, because I now, when my
function would return a _Reference_ (&), then I _know_
I can say info.GetInfo().c_str().

But the function returns an object by value!?!?

Thanks for help!

Nathan



Mon, 22 Apr 2002 03:00:00 GMT  
 Simple C++ question: Using return values (by value)
You cannot assign an STL-string "as is" to an MFC-string. That's why you
must use ".c_str()". Everything is all right.

But for my experience I would not use STL and MFC together because of
exactly these differencies in notation. Also lots of code are implemented
twice. Mixing the two libraries blows up your code. If I build a library
without using MFC, then I use STL, too, but all the interface of the library
should then be STL-free (char* instead of string).

Torsten



Quote:
> Hi!

> I have a member function like:

> string LgInfo::GetInfo() {
>     string s("");
>    //...
>     return s;
> }

> Then I use lots of client code, that converts the return value
> in a c-string in order to assign to a MFC CString, like:

>     LgInfo info;
>     CString cS = info.GetInfo().c_str();   // using ".foo()" safe???

> Now, I am suddenly unsure about using the return value
> as an object and append this ".c_str()".

> Is this safe???
> Or does it work just in debug mode???

> Wyh am suddenly unsure is, because I now, when my
> function would return a _Reference_ (&), then I _know_
> I can say info.GetInfo().c_str().

> But the function returns an object by value!?!?

> Thanks for help!

> Nathan



Mon, 22 Apr 2002 03:00:00 GMT  
 Simple C++ question: Using return values (by value)
Thanks a lot, that was excatly the answer
to my question, and fast!

Nathan



Mon, 22 Apr 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Simple ATL Object-Returning a value to the calling object

2. Value, value, who's got the value?

3. Using the return value of main()

4. using return values or args

5. Returning a value other than zero using system()

6. Using enum as a return value in a custom [local] interface method

7. Getting the return value of a stored procedure using CRecordset

8. Using ADO variable returns junk value?

9. How to i get return values from a stored procedure in Visual C++

10. Help - Calling ActiveX DLL function from VB and C++ returns different values

11. ATL Web Server return values question?

12. simple question: passing values between dialog boxes

 

 
Powered by phpBB® Forum Software