'B' is an inaccessible base of 'D'

Discussion in 'C++' started by yuvalif@gmail.com, Dec 26, 2005.

  1. Guest

    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.
     
    , Dec 26, 2005
    #1
    1. Advertising

  2. John Carson Guest

    <> wrote in message
    news:
    > 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
    {
    };


    --
    John Carson
     
    John Carson, Dec 26, 2005
    #2
    1. Advertising

  3. Zara Guest

    On 25 Dec 2005 22:16:51 -0800, wrote:

    >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
     
    Zara, Dec 26, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    652
    codigo
    May 4, 2005
  2. Victor Bazarov
    Replies:
    15
    Views:
    761
    Greg Comeau
    Mar 4, 2007
  3. Stefan Weber
    Replies:
    9
    Views:
    419
    Stefan Weber
    May 29, 2007
  4. Stefan Weber
    Replies:
    2
    Views:
    420
    Howard
    May 22, 2007
  5. Stefan Weber
    Replies:
    0
    Views:
    381
    Stefan Weber
    May 22, 2007
Loading...

Share This Page