views:

112

answers:

3

Hi All!

I have the following code:

void Stack::operator =(Stack &rhs)
{
    //do the actual copying
}

Stack::Stack(Stack &rhs) //copy-constructor
{
    top=NULL; //initialize this as an empty stack (which it is)
    *this=rhs; //invoke assignment operator
}

Stack& Stack::CopyStack()
{
    return *this; //this statement will invoke copy contructor
}

It is being used like this:

unsigned Stack::count()
{
    unsigned c=0;
    Stack copy=CopyStack();
    while (!copy.empty())
    {
        copy.pop();
        c++;
    }
    return c;
}

Removing reference symbol from declaration of CopyStack (returning a copy instead of reference) makes no difference in visual studio 2008 (with respect to number of times copying is invoked). I guess it gets optimized away - normally it should first make a copy for the return value, then call assignment operator once more to assign it to variable sc.

What is your experience with this sort of optimization in different compilers?

Regards, Dženan

+2  A: 

This statement is copy-initialization of a Stack called copy from the return value of CopyStack(). There's no assignment.

Stack copy=CopyStack();

In this function the comment is incorrect. There is no invocation of the copy-constructor as the return value is a reference.

Stack& Stack::CopyStack()
{
    return *this; //this statement will invoke copy contructor
}

This means that the original initialization is, in effect, copy-construction from *this variable.

If the return value was by value then the copy-initialization would be from a temporary, but one which could validly be eliminated by the compiler.

I don't see the point in the CopyStack function. It would be more idiomatic to just perform a direct initialization:

Stack copy(*this);
Charles Bailey
Thanks for taking time to post an answer. The point of CopyStack method is the notation convenience, or maybe better answer is I first wrote CopyStack method, only to have it replaced by this combination of 3 methods once the original idea didn't work. The control comes to copy constructor, although later - and that comment was more to myself. I was more hoping someone had experience with GCC or some other compiler (on the point of optimization regarding such things) and could share it with us.
Dženan
A: 

If I understand correctly, you can prototype your stack objects and return prototypes instead, which may introduce some performance but you have to be careful about object ownership for memory resources.

You can use Prototype Factory design pattern, if you like.

baris_a
Thanks for the time taken, but you also seem to miss the point, just as Thomas (see comment to the first post) and Charles Bailey.
Dženan
A: 

I guess what you're looking for is RVO (Return Value Optimization) and NRVO (Named Return Value Optimization). Compiler optimization is a vast subject, and if I were to start typing a description it would take toooo long! Just check out this blog post... It explains it nicely.

themoondothshine
GCC, BTW, applies these two algorithms, as do most other compilers. Also, you might be interested in taking a look at C++0x's 'move semantics'. Move semantics enables elimination of unnecessary copying of objects by just making use of the fact that it is no point in copying a temporary object which is not referenced by anything else and is going to be destroyed shortly.
themoondothshine
Well actually I am surprised that compiler did a good optimization of my code, with all the criticism of C++ out there.
Dženan
Criticism?? Well, to me C++ has always been a very powerful language (and I detest managed code!), but hey... there are two sides to every story, aren't there?
themoondothshine
I have taken a look at C++0x a few times already - and I think it is great, now I just wait for final specification to appear, and to be implemented.
Dženan
C++ is my mother programming language too. I had fun reading some of this: http://yosefk.com/c++fqa/
Dženan