在C ++中继承和覆盖ostream运算符 [英] Inheriting and overriding ostream operator in C++

查看:282
本文介绍了在C ++中继承和覆盖ostream运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一个答案,但没有人会有和我一样的问题。



我使用几个派生类。 ostream运算符<<对于每一个都应该打印出一些共同的东西,每一个,和一些事情具体到每个。后来,我想从这些派生类进一步派生,再次新的派生类需要打印出一些在它们之上的世代的东西。

例如:



基本类.h文件

  class Base 

{



int FirstClassNumber;

//我目前使用的声明,朋友给了我
//我很确定我的问题在这里。


public:

friend ostream&运算符<< (ostream& os,const Base& base)
{
base< os;

return os;
}

virtual void operator<< (ostream& os)const = 0;

};

Base.cpp文件包括以下行:

  void Base :: operator<< (ostream& os)
{
os<< FirstClassNumber;
}

然后我派生:(FirstDerived.h)

  class FirstDerived:public Base 

{

int SecondClassNumber;

};

FirstDerived.cpp



  FirstDerived :: operator<< (ostream& os)
{
os<<

第一个数字是:

//这是不工作的行 - 别人给我这个语法

< < Base :: operator<<<

<<第二个数字是

<< SecondClassNumber;
}

然后我想导出:

 code> class SecondDerived:Public FirstDerived 
{

int ThirdClassNumber;

};

Second.cpp:

  FirstDerived :: operator< ;(ostream& os)
{
os <<

FirstDerived :: operator<<

<< number是

<< ThirdClassNumber;

}

我认为问题很可能是在程序的最开始的声明,或者像 Base :: operator<< 。 p>

另一个可能性是我不会在每个继承类的.h文件中重新声明它。我应该是,如果是,我应该使用什么语法?



建议我使用 static_cast 方法,但我的教授因此不会给我们太多的帮助)说,有一个更好的方法来做到这一点。任何建议?

解决方案

一个简单的方法是:

  class Base 
{
int FirstClassNumber;

public:
virtual void serialize(ostream& os)const
{
os< FirstClassNumber;
}
};

//为基类实现流运算符。
//所有它是调用erialize这是一个虚拟方法
//将调用最导出的版本。
ostream&运算符<< (ostream& os,const Base& base)
{
base.serialize(os);

return os;
}

class FirstDerived:public Base
{
int SecondClassNumber;

public:
//覆盖serialize以使其调用基本版本。
//然后输出任何本地数据。
virtual void serialize(ostream& os)const
{
Base :: serialize(os);
os<< SecondClassNumber;
}
};


I've been trying to find an answer to this, but no one seems to have exactly the same problem as I do.

I am working with several derived classes. The ostream operator << for each of these should print out some things common to each, and some things specific to each. Later on, I would like to further derive from these derived classes, and again the new derived classes need to print out some things that are in the "generations" above them.
For example:

The Base class .h file

class Base

{  



 int FirstClassNumber;

//The declaration I'm currently working with, that a friend gave me
//I'm pretty sure my problem lies here.


public:

friend ostream& operator << (ostream& os, const Base &base)
{
    base << os ;

    return os;
}

virtual void operator << (ostream& os) const = 0;

};

The Base.cpp file includes these lines:

void Base::operator << (ostream& os)
{
  os << FirstClassNumber;
}

Then I derive: (FirstDerived.h)

class FirstDerived : Public Base

{ 

int SecondClassNumber;

};

FirstDerived.cpp:

FirstDerived::operator << (ostream& os)
{
  os <<

  "The first Number is:

 //This is the line that isn't working - someone else gave me this syntax

  << Base::operator<< 

  << "The second number is"

  << SecondClassNumber;
}

Then I want to derive:

class SecondDerived: Public FirstDerived
{ 

int ThirdClassNumber;

};

Second.cpp:

FirstDerived::operator << (ostream& os)
{
  os <<

 FirstDerived::operator<<

 << "The third number is "

 << ThirdClassNumber;

 }

I think the problem is most likely either the declaration in the very start of the program, or the lines like Base::operator<<.

Another possibility is that I'm not redeclaring it in the .h file of every inherited class. Should I be, and if so what syntax should I use?

It was suggested to me to use the static_cast method, but my professor (the one who wrote the assignment, and therefore won't give us too much help with it) said that there's a better way to do it. Any suggestions?

解决方案

A simple technique for this is:

class Base
{  
    int FirstClassNumber;

    public:
        virtual void serialize(ostream& os) const
        {
             os << FirstClassNumber;
        }
};

// Implement the stream operator for the base class.
// All it does is call erialize which is a virtual method that
// will call the most derived version.
ostream& operator << (ostream& os, const Base &base)
{
    base.serialize(os);

    return os;
}

class FirstDerived:public Base
{  
    int SecondClassNumber;

    public:
        // Override serialize to make it call the base version.
        // Then output any local data.
        virtual void serialize(ostream& os) const
        {
             Base::serialize(os);
             os << SecondClassNumber;
        }
};

这篇关于在C ++中继承和覆盖ostream运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆