'B' is an inaccessible base of 'D'

Y

yuvalif

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.
 
J

John Carson

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.

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
{
};
 
Z

Zara

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

This is equivalent to class D0: public BB, private 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",

This is equivalent to class D0: public B, private BB
everything works fine.
Why?

TID, Yuval.

Regards,
-- Zar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top