Re: inheritance
From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 02/26/04
- Next message: Albert van der Horst: "Re: time to get rid of unsigned?"
- Previous message: Albert van der Horst: "Re: time to get rid of unsigned?"
- In reply to: vsgdp: "inheritance"
- Next in thread: vsgdp: "Re: inheritance"
- Reply: vsgdp: "Re: inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 26 Feb 2004 00:45:37 GMT
"vsgdp" <nospam@nospam.com> wrote...
> Consider:
>
> #include <iostream>
> using namespace std;
> class A
> {
> public:
> A(){ p = this; }
> static A* p;
> };
> A* A::p = 0;
>
> class B : public A
> {
> public:
> B() : A() {}
> };
>
> int main()
> {
> B b;
> if( b.p == &b )
> cout << "Equal." << endl;
> }
> Output: Equal.
>
> Why does b.p point to b?
It doesn't.
> It seems it should point to the "A" part of b.
And it does.
> That is, Stroustrup says "In this respect, the base class acts exactly
like
> a member of the dervied class." (page 306 in special edition).
>
> However, the behavior of the program can be explained if we think of A()
as
> a "method" of B. But is that technically correct?
No.
> Accelerated C++ says
> constructors of a base class are not also members of the derived class.
The reason that the condition
b.p == &b
evaluates to 'true' is simple: both sides need to be of the same type to
participate in comparison. The left side is of type A*. So, the right
side is _converted_ to A* using the standard "pointer to derived to pointer
to base" conversion.
Your mistake is to think of addresses as typeless. They aren't.
Victor
- Next message: Albert van der Horst: "Re: time to get rid of unsigned?"
- Previous message: Albert van der Horst: "Re: time to get rid of unsigned?"
- In reply to: vsgdp: "inheritance"
- Next in thread: vsgdp: "Re: inheritance"
- Reply: vsgdp: "Re: inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|