Thursday, November 14, 2013

Returning a class instance by value and a naming question

Newsgroup: comp.lang.c++

Subject: Returning a class instance by value and a naming question

From: DSF <notavalid@...>

Date: Wed, 13 Nov 2013 18:09:08 -0500



Hello,



I have a class I've been writing where I frequently need to return

the class by value so that class members can be changed without

affecting the original. The problem is all of the copying. First,

the class has to be copied into a local instance of the class. Then,

upon return, the local instance is copied into an unnamed instance

that's created before the function is called, and therefore not

destroyed when the function ends. Finally, after the function ends,

the operator= of the target is called, yet again copying the class

(unnamed). Here is an example:



FStringW FStringW::Start(size_t end) const

{

FStringW tfs(*this);

if(end < length)

{

tfs.str[end] = 0;

tfs.length = end;

}

tfs.StartIP(end);

return tfs;

}



StartIP is a member function that alters the string in place.



It returns 'end' number of characters of the start of a wide string.



I tried creating a "global" temp that could be used in place of each

function that used 'tfs'. It was a pointer to FStringW that was

created in each constructor and deleted in the destructor. I managed

to avoid an infinite loop in the constructor, but didn't realize it

created one in the destructor, too. I wasn't able to get around that

one. All the functions that returned FStringW returned a reference to

the global object, so the reference had the lifetime of until another

function uses it. That was acceptable. It would have eliminated one

copy if it worked.



So the question is: Are there any techniques for minimizing the

number of copies that occur when returning by value?



The ideal situation would be if the returned object could become the

unnamed object that's passed to operator=. But I think the compiler

would have to do that. And there's always the chance the function

could return an expression i.e. return tfs1 + tfs2.



My second question relates to the naming of member functions. Most

string-altering functions I've written have two versions, one that

returns by value and leaves the original alone, and one that alters

the string in place. For now, I'm using:



FStringW FStringW::Start(size_t end);

void FStringW::StartIP(size_t end);



But I was thinking maybe something like:



FStringW FStringW::GetStringStart(size_t end);

void FStringW::StringStart(size_t end);



Something like Crop or Cut StringEnd is more descriptive, but uses

start and end as terms for functions that do very similar things. Any

Ideas?



Thanks.

"'Later' is the beginning of what's not to be."

D.S. Fiscus







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?128115-Returning-a-class-instance-by-value-and-a-naming-question&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment