Why would the conversion between derived* to base* fails with private inheritance?

后端 未结 7 500
栀梦
栀梦 2020-12-09 04:06

Here is my code -

#include
using namespace std;

class base
{
public:
    void sid()
    {
    }  
};

class derived : private base
{
public:         


        
7条回答
  •  执念已碎
    2020-12-09 04:11

    Try this:

    #include
    #include
    using namespace std;
    
    class base
    {
          private:
          public:
              virtual void sid() // You might want to declare sid virtual
                 {
                      cout<<"base";
                 } 
              virtual ~base() // You then probably need a virtual destructor as well.
                 {
                 } 
    };
    
    class derived : public base //public inheritance
    {
          private:
          public:
                 void sid()
                 {
                      cout<<"derived";
                 }
    };
    
    int main()
    {
        base * ptr;
        ptr = new derived;
        ptr->sid();
        getch();
        return 0;
    }
    

提交回复
热议问题