Private Inheritance is not subtyping

Discussion in 'C++' started by Ragnar, Nov 7, 2003.

  1. Ragnar

    Ragnar Guest

    Hello

    If there a derived class privately inheriting from a base class, there
    is no subtyping relationship between them. So a funtion that expects a
    base class pointer should not accept a derived class pointer.

    However, if this same function is called from within a derived class
    function, passing 'this', then the function accepts it. Why is that. I
    enclose some test code:

    #include <iostream>

    using std::cout;

    class Base {
    friend class FriendClass;
    private:
    virtual void print() const { cout << "In Base\n"; }
    };

    class FriendClass {
    public:
    void print(Base * pb) { pb->print(); }
    };

    class Derived : private Base { // note private
    void print() const { cout << "In Derived 2\n"; }
    public:
    void printEx(FriendClass & rf, Derived * pd) {
    rf.print(pd); // why does this work ?
    rf.print(this); // why does this work ?
    }
    };

    int main()
    {
    FriendClass of;
    Derived od;
    //Base * pb = new Derived; // will not work, base inaccessible
    //of.print(&od); // does not work
    od.printEx(of, &od); // works

    return 0;
    }

    Here, if print is called from main, being passed &od, it does not
    compile. If print is called from printEx, being passed this, it
    compiles. Why ?

    I used g++ 3.2.

    Regards & Thanks

    --
    Ragnar
     
    Ragnar, Nov 7, 2003
    #1
    1. Advertising

  2. On 7 Nov 2003 01:31:40 -0800, Ragnar <> wrote:

    > Hello
    >
    > If there a derived class privately inheriting from a base class, there
    > is no subtyping relationship between them. So a funtion that expects a
    > base class pointer should not accept a derived class pointer.
    >
    > However, if this same function is called from within a derived class
    > function, passing 'this', then the function accepts it. Why is that. I
    > enclose some test code:
    >
    > #include <iostream>
    >
    > using std::cout;
    >
    > class Base {
    > friend class FriendClass;
    > private:
    > virtual void print() const { cout << "In Base\n"; }
    > };
    >
    > class FriendClass {
    > public:
    > void print(Base * pb) { pb->print(); }
    > };
    >
    > class Derived : private Base { // note private
    > void print() const { cout << "In Derived 2\n"; }
    > public:
    > void printEx(FriendClass & rf, Derived * pd) {
    > rf.print(pd); // why does this work ?
    > rf.print(this); // why does this work ?


    conversion Derived* -> Base* is private in Derived, will not be inherited
    but exist in Derived.
    Function print in FriendClass calls print through the pointer to Base,
    that is Derived::print will be called.


    > }
    > };
    >
    > int main()
    > {
    > FriendClass of;
    > Derived od;
    > //Base * pb = new Derived; // will not work, base inaccessible
    > //of.print(&od); // does not work
    > od.printEx(of, &od); // works
    > return 0;
    > }
    >
    > Here, if print is called from main, being passed &od, it does not
    > compile. If print is called from printEx, being passed this, it
    > compiles. Why ?
    >
    > I used g++ 3.2.
    >
    > Regards & Thanks
    >
    > --
    > Ragnar
    >




    --
    grzegorz
     
    Grzegorz Sakrejda, Nov 7, 2003
    #2
    1. Advertising

  3. Ragnar

    Dan Cernat Guest

    (Ragnar) wrote in message news:<>...
    > Hello
    >
    > If there a derived class privately inheriting from a base class, there
    > is no subtyping relationship between them. So a funtion that expects a
    > base class pointer should not accept a derived class pointer.
    >
    > However, if this same function is called from within a derived class
    > function, passing 'this', then the function accepts it. Why is that. I
    > enclose some test code:


    see in code

    >
    > #include <iostream>
    >
    > using std::cout;
    >
    > class Base {
    > friend class FriendClass;
    > private:
    > virtual void print() const { cout << "In Base\n"; }
    > };
    >
    > class FriendClass {
    > public:
    > void print(Base * pb) { pb->print(); }
    > };
    >
    > class Derived : private Base { // note private
    > void print() const { cout << "In Derived 2\n"; }
    > public:
    > void printEx(FriendClass & rf, Derived * pd) {
    > rf.print(pd); // why does this work ?

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pd is Derived* so it calls the Derived::print

    > rf.print(this); // why does this work ?

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is Derived* so it calls the Derived::print

    > }
    > };
    >
    > int main()
    > {
    > FriendClass of;
    > Derived od;
    > //Base * pb = new Derived; // will not work, base inaccessible
    > //of.print(&od); // does not work
    > od.printEx(of, &od); // works
    >
    > return 0;
    > }
    >
    > Here, if print is called from main, being passed &od, it does not
    > compile. If print is called from printEx, being passed this, it
    > compiles. Why ?


    the output of the program is:

    In Derived 2
    In Derived 2


    >
    > I used g++ 3.2.
    >
    > Regards & Thanks


    It does what is supposed to do. Check again your code.

    /dan
     
    Dan Cernat, Nov 7, 2003
    #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. valentin tihomirov

    Subtyping issue

    valentin tihomirov, Jun 30, 2005, in forum: VHDL
    Replies:
    1
    Views:
    554
    Jonathan Bromley
    Jun 30, 2005
  2. qazmlp
    Replies:
    19
    Views:
    825
    Daniel T.
    Feb 4, 2004
  3. shawn
    Replies:
    2
    Views:
    277
    shawn
    Oct 17, 2005
  4. Replies:
    3
    Views:
    387
  5. Michael Le Barbier Grünewald

    Subtyping iterators

    Michael Le Barbier Grünewald, May 9, 2012, in forum: C++
    Replies:
    5
    Views:
    419
Loading...

Share This Page