Re: [Dev-C++] Copy Constructors and Overloaded Assignment Operators
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Matthew H. <mhi...@sy...> - 2002-07-20 23:40:59
|
Hello Ioannis,
>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. :)
>
>
No probably not. However, I've got enough to get me going for now. 8-)
Thanks for the explanation. const for const... Another rule to tag on
to using static members for
callbacks.
>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;
>}
>
>
True. The only thing I don't like about that is that any validation
I've put
in place doesn't get triggered. I know by that stage in the game that
everything should be okay... but if there are some triggers (like logging,
computation, etc.... I'd lose them). I mean, I could use an initializer
list
too, right? (That always seemed counter-intuitive to me though...
Decreases legibility, unless you're used to it... and same problem as
above...).
Now, speaking of class members, how would you persist state?
In VB (the realm I come from... well, there was Pascal those many
years ago 8-), I would use a Type (read struct) to hold my data.
This allows for quick memory copies to restore defaults/back out
changes... In C++ I can also see it making serialization a little
easier (convert to a char array/string and dump to disk).
Any thoughts on alternative measures to accomplish the same?
(I'm playing with a few projects that might make use of something
like this, but want to make sure that I'm not starting off on the
wrong foot...)
>Keep coding,
>
>
I will. 8-)
|