Topic: Copying and Inheritance
Author: tohoyn@janus.otol.fi (Tommi H|yn{l{nmaa)
Date: 4 Oct 1994 19:29:28 GMT Raw View
Suppose that I have the following code:
class A
{
public:
A(const A&);
A(int);
A& operator = (const A&);
A& operator = (int);
...
};
int operator == (const A&, const A&);
void DoSomething(A a);
class B : public A
{
public:
B(const B& b) : A(b) {} // *1*
B(int n) : A(n) {} // *2*
...
};
void main()
{
B b1(1), b2(2);
b1 = b2; // *3*
b2 = 3; // *4*
if( b1 == b2 ) // *5*
{
b1 = 0;
b2 = 0;
}
DoSomething(b1); // *6*
}
Questions:
* Are the points *1* and *2* legal?
* Do the points *3* and *4* invoke the 'A::operator = (...)' or
'B::B(...)' ?
Does the class B inherit the 'operator =''s of the class A ?
* How are the B's (b1 and b2) converted to A's in the point *5*, where the
'int operator == (const A&, const A&)' is called ?
Does the 'operator ==' get the base classes of b1 and b2 or are they copied
to temporary objects of class A using 'A::A(const A&)' ?
* How is b1 converted to an object of class A in the point *6* ?
Author: jason@cygnus.com (Jason Merrill)
Date: Tue, 4 Oct 1994 21:49:23 GMT Raw View
>>>>> Tommi H|yn{l{nmaa <tohoyn@janus.otol.fi> writes:
> Suppose that I have the following code:
> class A
> {
> public:
> A(const A&);
> A(int);
> A& operator = (const A&);
> A& operator = (int);
> ...
> };
> int operator == (const A&, const A&);
> void DoSomething(A a);
> class B : public A
> {
> public:
> B(const B& b) : A(b) {} // *1*
> B(int n) : A(n) {} // *2*
> ...
> };
> void main()
> {
> B b1(1), b2(2);
> b1 = b2; // *3*
> b2 = 3; // *4*
> if( b1 == b2 ) // *5*
> {
> b1 = 0;
> b2 = 0;
> }
> DoSomething(b1); // *6*
> }
> Questions:
> * Are the points *1* and *2* legal?
Yes. Why wouldn't they be?
> * Do the points *3* and *4* invoke the 'A::operator = (...)' or
> 'B::B(...)' ?
*3* invokes the synthesized B::operator=(const B&). *4* invokes
B::B(int), and then B::operator=(const B&).
> Does the class B inherit the 'operator =''s of the class A ?
No.
> * How are the B's (b1 and b2) converted to A's in the point *5*, where the
> 'int operator == (const A&, const A&)' is called ?
> Does the 'operator ==' get the base classes of b1 and b2 or are they copied
> to temporary objects of class A using 'A::A(const A&)' ?
Base classes.
> * How is b1 converted to an object of class A in the point *6* ?
By A::A(const A&).
Jason
Author: tohoyn@freenet.hut.fi (Tommi Hoynalanmaa)
Date: 4 Oct 1994 20:23:24 GMT Raw View
Suppose that I have the following code:
class A
public:
A(const A&);
A(int);
A& operator = (const A&);
A& operator = (int);
...
;
int operator == (const A&, const A&);
void DoSomething(A a);
class B : public A
public:
B(const B& b) : A(b) // *1*
B(int n) : A(n) // *2*
...
;
void main()
B b1(1), b2(2);
b1 = b2; // *3*
b2 = 3; // *4*
if( b1 == b2 ) // *5*
b1 = 0;
b2 = 0;
DoSomething(b1); // *6*
Questions:
* Are the points *1* and *2* legal?
* Do the points *3* and *4* invoke the 'A::operator = (...)'
or 'B::B(...)' ?
Does the class B inherit the 'operator =''s of the class A ?
* How are the B's (b1 and b2) converted to A's in the point *5*,
where the 'int operator == (const A&, const A&)' is called ?
Does the 'operator ==' get the base classes of b1 and b2 or
are they copied to temporary objects of class A using
'A::A(const A&)' ?
* How is b1 converted to an object of class A in the point *6* ?
--