Re: Function Returning a local object???
From: velthuijsen (velthuijsen_at_hotmail.com)
Date: 09/07/04
- Next message: JosephWu: "Re: a flat distribution"
- Previous message: John Harrison: "Re: how make cast from ifstream to istream in MS Visual C++"
- Maybe in reply to: Rich Herrick: "Re: Function Returning a local object???"
- Next in thread: John Harrison: "Re: Function Returning a local object???"
- Reply: John Harrison: "Re: Function Returning a local object???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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;
}
- Next message: JosephWu: "Re: a flat distribution"
- Previous message: John Harrison: "Re: how make cast from ifstream to istream in MS Visual C++"
- Maybe in reply to: Rich Herrick: "Re: Function Returning a local object???"
- Next in thread: John Harrison: "Re: Function Returning a local object???"
- Reply: John Harrison: "Re: Function Returning a local object???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|