Re: Function Returning a local object???

From: velthuijsen (velthuijsen_at_hotmail.com)
Date: 09/07/04


Date: 7 Sep 2004 05:11:20 -0700


--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--
> This is no problem, because I can imagine the return value of foo()
> i.e. 5, being copied to the integer bar. But when the foo() returns a
> derived class class whose member variables are themselves class
> objects or worse still objects(?) of a derived class, I suspect there
> will be a fair amount of copying taking place when foo returns such a
> class by value. My question therefore how is this all the necessary
> copying handled??? By the overlaoded assignament operator, operator= ?

The best way to find out is to write small programs to see what
happens when you try out the different options. At the bottom I've got
a small program to get you started.

> If so, what happens is this operator= has not been defined by the
> programmer. Is it generated by the compiler? (I've read enough to know
> that the copy constructor is only invloved in the initialization of
> newly created variables, global or otherwise, but this is not the case
> here. All that's happening here is assignment.)

Yes if you do not define the operator= the compiler will provide one
for you. What is does is byte copying. The compiler will also create
the constructor, destructor and copy constructor if you don't defined
them.

#include <iostream>

using std::cout;

static int base = 1;
class Foo
{
public:
        Foo(int i = base) {intern = i; cout << intern << " Foo Ctor\n";}
        ~Foo() { cout << intern << " Foo Dtor\n"; }
        Foo(const Foo &Right) {intern = Right.intern; cout << intern << " "
<< Right.intern << " Foo copy Ctor\n"; }
        Foo& operator= (const Foo &Right) { cout << intern << " " <<
Right.intern << " Foo operator=\n"; intern = Right.intern; return
*this;}
        Foo operator+ (const Foo &Right) { cout << "Foo operator+\n"; Foo
ter(*this); ter.intern += Right.intern; return ter;}
private:
        int intern;
};

int main()
{
        {
                Foo Bar;
                ++base;
                Foo Bar2(Bar);
                ++base;
                Foo Bar3 = Bar2;
                ++base;
                Foo Bar4;
                ++base;
                Foo Bar5;
                ++base;
                Foo Bar6 = Bar5+ Bar4;
                ++base;
                Bar4 = Bar2 = Bar6;
        }
        getchar();
        return 0;
}



Relevant Pages

  • Re: Function Returning a local object???
    ... >>are themselves class objects or worse still objectsof a derived class, ... >>when foo returns such a class by value. ... the compiler will emit code similar to: ... >>(I've read enough to know that the copy constructor is only invloved ...
    (comp.lang.cpp)
  • Re: Cannot add new to a empty table by using IADORecordBinding interfa
    ... adFldOk between calling pInterface->AddNewand ... Also it helps if all your member variables are ... equivalent to 0 in the constructor of the derived class from ...
    (microsoft.public.data.ado)
  • Cant refer to base class attribute?
    ... I've got a Base class with an attribute "foo", ... Derived class. ... In Derived's constructor, I try to ... refer to Base.foo, but python complains: ...
    (comp.lang.python)
  • Re: C++ design question
    ... so anyone can instantiate it without ... > default constructor in the class header rather that allowing it to be ... > separately and initialize a pointer to it in Foo: ... fooDeriveN::DoStuffWithBarBase() at all because we have a Bar* in Foo*. ...
    (comp.object)
  • Re: C++ design question
    ... The default constructor is implied in the initialization list. ... One wants to access Bar at the superclass level to avoid static typing ... Usually that would be done by whoever instantiates a Foo subclass ...
    (comp.object)