【问题标题】:Array of pointers to the base class that contains objects of derived classes and overloaded << operator指向包含派生类对象和重载 << 运算符的基类的指针数组
【发布时间】:2015-07-28 13:51:45
【问题描述】:

我有以下问题。

我已经创建了一个指向基类对象的指针数组,但是我在这个数组中还存储了指向派生类对象的指针。

我还在每个类中重载了&lt;&lt;operator 以显示对象。 但是,当我将这个重载的&lt;&lt;operator 应用到上述数组时,它会将所有指针视为指向基类的对象。

下面我给出了描述问题的代码。 我需要这个重载的运算符才能正常工作,因为我需要将数组指向的对象保存在文件中。

    #include <iostream> 
    #include <cstdio>
    #include <cstdlib>

    using namespace std;

    class Base
    {
       public:
       int basevar;
       Base(): basevar(1) {};
       virtual void dosth(){};
       friend ostream & operator<<(ostream & screen, const Base & obj);
    };

    ostream & operator<<(ostream & screen, const Base & obj)
    {
       screen << obj.basevar;
       return screen;
    };

    class Der1: public Base
    { 
        public:
        int de1;
        Der1(): de1(2) {};
        virtual void dosth()
        {
           cout << "Der1" << endl;
        }
        friend ostream & operator<<(ostream & screen, const Der1 & obj);
    };

    ostream & operator<<(ostream & screen, const Der1 & obj)
    {
       Base b;
       b = static_cast <Base>(obj); 
       screen << b; 
       screen << " " << obj.de1;
       return screen;
    };

    class Der2: public Base
    { 
        public:
        int de2;
        Der2(): de2(3) {};
        virtual void dosth()
        {
           cout << "Der2" << endl;
        }
        friend ostream & operator<<(ostream & screen, const Der2 & obj);
    };

    ostream & operator<<(ostream & screen, const Der2 & obj)
    {
        Base b;
        b = static_cast <Base>(obj); 
        screen << b; 
        screen << " " << obj.de2; 
        return screen; 
    }

    int main()
    {
         Base * array[] = {new Base(), new Der1(), new Der2()};
         for(int i=0; i<3; ++i)
         {
             cout << *array[i]; // <- always displays objects as if they were from the base class
         }
         return 0;
    }

【问题讨论】:

标签: c++ pointers


【解决方案1】:

你可以通过以下方式在基类中声明一个虚函数

class Base
{
   public:
   int basevar;
   Base(): basevar(1) {};
   virtual std::ostream & out( std::ostream &os ) const
   {
       return os << basevar;
   }

   virtual void dosth(){};
   friend ostream & operator<<(ostream & screen, const Base & obj);
};

在这种情况下,运算符看起来像

ostream & operator<<(ostream & screen, const Base & obj)
{
    return obj.out( screen );
};

并在派生类中重新定义虚函数。例如

class Der1: public Base
{ 
    public:
    int de1;
    Der1(): de1(2) {};
    std::ostream & out( std::ostream &os ) const
    {
       return Base::out( os ) << " " << obj.de1;
    }
    virtual void dosth()
    {
       cout << "Der1" << endl;
    }
};

在这种情况下,无需为派生类定义运算符。

【讨论】:

    猜你喜欢
    • 2016-03-17
    • 2014-06-16
    • 2014-09-23
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多