Brett McCoy wrote:
> On Wed, May 6, 2009 at 12:07 PM, mark <[email protected]> wrote:
>> Mark.h
>>
>> #pragma once //i am using visual c++ 2008 compiler/ide, this is
>> equvilaent to #ifndef mark_h... etc...
>> #include <string> //is this part needed here?
>> class MyClass
>> {
>> public:
>> int id;
>> MyClass(int);
>>
>> ~MyClass(){}
>>
>> string func() // i want this function to return the string below,
>> but it doesnt
>> {
>> return "6767";
>> }
>
>
> string func()
> {
> string newString = "6767";
> return newString;
> }
>
> Be careful, of course, returning references to local variables (in
> this case, we are returning a copy of the local variable).
>
> -- Brett
> ------------------------------------------------------------
> "In the rhythm of music a secret is hidden;
> If I were to divulge it, it would overturn the world."
> -- Jelaleddin Rumi
>
>
> ------------------------------------
Or, more succinctly:
std::string func()
{
return std::string("6767");
}
Instead of initializing, assigning, and then returning a copy. More
opportunity for the compiler to optimize and inline the function.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/