Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 26th, 2005, 06:25 AM
yuvalif@gmail.com
Guest
 
Posts: n/a
Default 'B' is an inaccessible base of 'D'

Hi All,
I fail to compile the following code (well, not exactly that code...):

class BB
{
};

class B
{
public:
static B* Create(int i);
};

class D0 : public BB, B
{
};

class D1 : public BB, B
{
};


B* B::Create(int i)
{
if (i == 0)
return new D0();
else
return new D1();
}

int main()
{
B *b = B::Create(1);
delete b;
return 0;
}

And get: "error: 'B' is an inaccessible base of 'D0', 'B'
is an inaccessible base of 'D1' "
But when I change the order of inheritance: "class D0 : public B, BB",
everything works fine.
Why?

TID, Yuval.

  #2  
Old December 26th, 2005, 07:55 AM
John Carson
Guest
 
Posts: n/a
Default Re: 'B' is an inaccessible base of 'D'

<yuvalif@gmail.com> wrote in message
news:1135577811.296988.284900@o13g2000cwo.googlegr oups.com[color=blue]
> Hi All,
> I fail to compile the following code (well, not exactly that code...):
>
> class BB
> {
> };
>
> class B
> {
> public:
> static B* Create(int i);
> };
>
> class D0 : public BB, B
> {
> };
>
> class D1 : public BB, B
> {
> };
>
>
> B* B::Create(int i)
> {
> if (i == 0)
> return new D0();
> else
> return new D1();
> }
>
> int main()
> {
> B *b = B::Create(1);
> delete b;
> return 0;
> }
>
> And get: "error: 'B' is an inaccessible base of 'D0', 'B'
> is an inaccessible base of 'D1' "
> But when I change the order of inheritance: "class D0 : public B, BB",
> everything works fine.
> Why?
>
> TID, Yuval.[/color]

You are apparently assuming that access qualifiers like

public BB, B

work in the same way as variable declarations, e.g.,

int x, y

i.e., that they continue in effect for identifiers after the first. They
don't. The access qualifier only has effect on the class that immediately
follows. If you omit a declaration, then you get the default access, which
is private in this case. Do it this way:

class D0 : public BB, public B
{
};

class D1 : public BB, public B
{
};


--
John Carson


  #3  
Old December 26th, 2005, 08:05 AM
Zara
Guest
 
Posts: n/a
Default Re: 'B' is an inaccessible base of 'D'

On 25 Dec 2005 22:16:51 -0800, yuvalif@gmail.com wrote:
[color=blue]
>Hi All,
>I fail to compile the following code (well, not exactly that code...):
>
>class BB
>{
>};
>
>class B
>{
>public:
> static B* Create(int i);
>};
>
>class D0 : public BB, B[/color]

This is equivalent to class D0: public BB, private B[color=blue]
>{
>};
>
>class D1 : public BB, B
>{
>};
>
>
>B* B::Create(int i)
>{
> if (i == 0)
> return new D0();
> else
> return new D1();
>}
>
>int main()
>{
> B *b = B::Create(1);
> delete b;
> return 0;
>}
>
>And get: "error: 'B' is an inaccessible base of 'D0', 'B'
>is an inaccessible base of 'D1' "
> But when I change the order of inheritance: "class D0 : public B, BB",[/color]

This is equivalent to class D0: public B, private BB
[color=blue]
>everything works fine.
>Why?
>
>TID, Yuval.[/color]

Regards,
-- Zar
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles