> Programming Languages > C++
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on C++



C++ - "Visibility of members in inheritance" in Programming Languages


Old 06-20-2004   #1
..i..
 
Default Visibility of members in inheritance

cl*** A
{
public:
method_A() { cout << "A method " << endl; }
};

cl*** B : public A
{
public:
method_B() { cout << "B method " << endl; }
};

int main()
{
A* aptr = new B;
....
}

Why cann't "aptr" see method_B() of cl*** B since "aptr" is a pointer
pointing actually at an object of type B?

Thanks in advance!



 
Old 06-20-2004   #2
..ct.. ..zar..
 
Default Re: Visibility of members in inheritance

"ctick" <ctick@flare.com> wrote...
> cl*** A
> {
> public:
> method_A() { cout << "A method " << endl; }


You mean

void method_A() { ...

> };
>
> cl*** B : public A
> {
> public:
> method_B() { cout << "B method " << endl; }


You mean

void method_B() { ...

> };
>
> int main()
> {
> A* aptr = new B;
> ...
> }
>
> Why cann't "aptr" see method_B() of cl*** B since "aptr" is a pointer
> pointing actually at an object of type B?


Because the _static_ type of aptr is A*. And it is not "pointing
actually" at a B, it is pointing to an A inside the B.

If you want to get to 'method_B', you will have to static_cast your
'aptr' to 'B*':

static_cast<B*>(aptr)->method_B();

Victor


 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0