RE: [Dev-C++] Copy Constructors and Overloaded Assignment Operators
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2002-07-20 23:00:44
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Matthew Hickson
> Sent: Sunday, July 21, 2002 12:33 AM
> To: Dev-C++ Mailing List
> Subject: Re: [Dev-C++] Copy Constructors and Overloaded
> Assignment Operators
>
>
> Hello Ioannis,
>
> Thanks!
>
> It's been awhile. 8-)
>
> Just what I needed. (Well, that and a rebuild all... 8-)
>
> Now, a little toast and jam to go with the egg on my face, and we'll
> have a meal!
To clarify things more.
You passed as an argument a const object of type Test. The member
functions of type Test that can act on a const object are only const
member functions. This applies of course to overloaded operators too
since they are member functions themselves (well, when you define them
as member functions). Do not forget to always declare all the
"read-only" member functions as const.
You can perform some magic on const objects using the mutable keyword,
but i do not think you have reached that level of consciousness yet. :)
Another interesting thing is that member functions of an object can
access the private part of objects of the same type.
So instead of that:
> // Operator =
> Test& Test::operator=(const Test& Test2){
> data = Test2.getData();
> data2 = Test2.getData2();
>
> return *this;
you can do directly that:
// Operator =
Test& Test::operator=(const Test& Test2){
data = Test2.data;
data2 = Test2.data2;
return *this;
}
Keep coding,
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|