Overloading << operator with polymorphism

有些话、适合烂在心里 提交于 2019-12-13 03:58:26

问题


I have read some similar questions and answers at SO, but don't really understand the answers there. My apologies, if this is a duplicate.

Have a base class like this.

class CParam
{
    public:
        virtual void PrintData(ostream &OutStream) = 0;
};

Now I inherit from this:

class CUInt32:public CParam
{
    public:
          void PrintData(ostream &OutStream);
}

void CUInt32::PrintData(ostream &OutStream)
{
    // Write some data to OutStream Here
}

I overload operator << for CUInt32 class

inline ostream   &operator<<(ostream &OutStream, CUInt32 &UInt32Obj)
{
    UInt32Obj.PrintData(OutStream);
    return (OutStream);
}

In int main() function I do the following:

int main()
{
    CParam *cp = new CUInt32(ANALOG);

    cout << *cp;// Error
    return 0;
}

I get an error saying error: no match for 'operator<<' in 'std::cout << *cp'|

My questions are

  1. Is it possible to use polymorphic base class pointers with cout?
  2. If yes, how we could do this?

Thanks for your time!


回答1:


The operator is not defined for the base class. Simply change this:

inline ostream   &operator<<(ostream &OutStream, CUInt32 &UInt32Obj)
{
    UInt32Obj.PrintData(OutStream);
    return (OutStream);
}

to this:

inline ostream   &operator<<(ostream &OutStream, CParam& cParam)
{
    cParam.PrintData(OutStream);
    return (OutStream);
}

Basically, defining the PrintData in the base class as virtual/abstract ensures it will be available for all subclasses and the correct function will be called.




回答2:


Along with the obvious point (operator<< should take a reference to a const base) you also want to change your definition of PrintData to make it a constmember function, so it can be invoked on a const object. The resulting code ends up something like this:

struct CParam {
    virtual void PrintData(ostream &OutStream) const = 0;
};

struct CUInt32 : CParam {
    void PrintData(ostream &OutStream) const {}
};

ostream &operator<<(ostream &OutStream, CParam const &UInt32Obj) {
    UInt32Obj.PrintData(OutStream);
    return (OutStream);
}



回答3:


Since overloading code uses PrintData, you could define the function at base class level itself. Since compiler couldn't find any overloading at base class level, it throws exception.



来源:https://stackoverflow.com/questions/17917345/overloading-operator-with-polymorphism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!