"Snake" <ha***@rogers.com> wrote...
Thanks alot guys for the help.That really clarified alot of thing.
But one more that I am not sure I really got.
In the example I mentiond(b = c).
do b and c each has spearated " identical" objects no matter how many
attributes are there?
Not necessarily. For all it's worth, b and c can be associated with
objects of two completely different classes, with the exception that
the b's type needs to define the assignment operator that accepts
the right side convertible from the c's type.
Or perhaps I just didn't understand the question...
ok,what if the object Test has a pointer d that refers to another
different object.
will b and c have two different pointers refering to the same object
referred by d previously or two pointers refereeing to two different
object?
Alright, so you say b and c are two different objects of the same type
that doesn't necessarily [re]define the assignment operator. And that
that type has a pointer as a member. Fine. During the assignment,
each member's assignment semantics will be followed (unless the operator=
is user-defined). For pointers that mean that after assignment is done,
they will point to the same object.
It can be circumvented, if you define your own assignment operator.
Example:
class Dummy {};
class NothingSpecial {
Dummy *p_;
public:
NothingSpecial(Dummy* p) : p_(p) {}
};
class OverloadedAssignment {
Dummy *p_;
public:
OverloadedAssignment(Dummy *p) : p_(p) {}
void operator =(OverloadedAssignment& oa) { } // p_ is left alone
};
int main() {
Dummy d1, d2;
NothingSpecial ns1(&d1), ns2(&d2); // ns1.p_ and ns2.p_ point to
// different Dummy objects
ns2 = ns1; // assignment semantics for pointers means now
// ns1.p_ and ns2.p_ point to the same object
OverloadedAssignment os1(&d1), os2(&d2); // point to different
os2 = os1; // overloaded assingment operator prevents copying
// of the p_ member -- they _STILL_ point to diff. obj
}
Victor