When is it not recommended to follow the link?

This is a memory allocation problem that I never understood.

void unleashMonkeyFish ()  
{  
    MonkeyFish * monkey_fish = new MonkeyFish ();
    std :: string localname = "Wanda";  
    monkey_fish-> setName (localname);  
    monkey_fish-> go ();  
}  

In the above code, I created a MonkeyFish object on the heap, assigned it a name, and then untied it around the world. Let's say that the ownership of the allocated memory was transferred to the MonkeyFish object itself, and only MonkeyFish will decide when to die and delete itself.

Now, when I define the data member "name" inside the MonkeyFish class, I can choose one of the following:

std :: string name;
std :: string & name;

When I define the prototype of the setName () function inside the MonkeyFish class, I can choose one of the following:

void setName (const std :: string & parameter_name);
void setName (const std :: string parameter_name);

I want to be able to minimize string copies. In fact, I want to completely destroy them, if I can. So it seems that I should pass the parameter by reference ... right?

What bothers me is that my local variable will go out of scope after the unleashMonkeyFish () function completes. Does this mean that I FORCED passed the parameter by copy? Or can I pass it by reference and "get away" somehow?

Basically, I want to avoid these scenarios:

  • MonkeyFish, localname , unleashMonkeyFish() . (, .)
  • , .

?

CLARIFICATION. , static, , unleashMonkeyFish(). , N MonkeyFish ( ), . ( , MonkeyFish - - , .)

EDIT: , , . , , ( ) , .

+5
9

-

std::string name;

. , unleashMonkeyFish , , ,

void setName( const std::string & parameter_name ) {
    name = parameter_name;
}

, - . , , . , . std::string . , "name.reserve(25)"; , , , , - . ( , , GCC , std::string, c-. , , ).

, unleashMonkeyFish, . - . , , , std::string . , , std::string local. , . std::string (msv++ afaik) : .

:

, , swap ( ):

void setName(std::string parameter_name) {
    name.swap(parameter_name);
}

, , , , . . ,

obj.setName("Mr. " + things.getName());

setName , , , , setName , - , . , . , , , , - , , .

BoostCon09/Rvalue-References

+6

:

void setName( const std::string & parameter_name );

:

std::string name;

setName:

name = parameter_name;

name , ( , setName).

, std::string, , , . , STL, .

+5

, MonkeyFish ( ) localname .

, , , , . , - . , ( , ), .

, ?

Edit

MonkeyName. MonkeyName . MonkeyName, ( *). . , MonkayName, , , , . , MonkeyName .

... , :)

+3

, . Boost shared_ptr < > , , , , .

, . .

+2

...

std::string localname = "Wanda";  

... ( ) emit 0x57 0x61 0x6E 0x64 0x61 0x00 [Wanda ] - . std::string (const char *) . const char *, / . MonkeyFish:: setName (const std::string &) std::string:: operator = (const std::string &), , std::string copy-on-write, , , .

, . ? , MonkeyFish? MonkeyFish , ? MonkeyFish, , , MonkeyFish const char *.

+2

, (const), .

1000- , , . OTOH , , , .

+2

, , . :

class MonkeyFish {
public:
  void setName( const std::string & parameter_name ) { name = parameter_name; }

private:
  std::string name;
};

, . , , , , , , , . , , - , .: -)

+2

unleashMonkeyFish , , ( , ).

"" (, #, Java) . , .

+1

( ), , , MonkeyFish, string, .

, , .

Assuming this is not an option, you can at least minimize the number of line copies to one. Pass the string as a reference to setName (), and then make a copy inside the setName () function itself. This way, you can be sure that the copy is executed only once.

+1
source

All Articles