You are not logged in.

1

Saturday, August 20th 2005, 7:11am

private inheritace

Hi All,

Pls look at this code

class a
{

};

class b:a
{

};

int main()
{

a *ptr = new b;

}

This pgm give the error as
error C2243: 'type cast' : conversion from 'class b *' to 'class a *' exists, but is inaccessible
Error executing cl.exe.

pls explain about this problem

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Saturday, August 20th 2005, 9:36am

RE: private inheritace

Change class b:a into class b:public a

3

Monday, August 22nd 2005, 6:12am

RE: private inheritace

why it's not worked when i used PRIVATE INHERITANCE??

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

4

Monday, August 22nd 2005, 8:59am

RE: private inheritace

Because you can't access 'a' as it is private :) It is like there is an inheritance but noone knows that, as none of 'a' members can be accessed:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class a{
public:
  void x();
};

class b: private class a {
public:
  void y();
};

a* i = new a();
b* j = new b();

i->x(); // it is valid
j->x(); // this is invalid, because x "doesn't exist" in b
// so:
((a*)j)->x() // this is invalid too, because x "doesn't exist" in b, 
             // so it can't be cast to a.