English 中文(简体)
正在超载 std:: ostream & 运算符 \\\\ 在导出类中, 没有静态(_cast) {} {}
原标题:overloading std::ostream& operator<< in the derived class without static_cast<>
  • 时间:2012-05-23 18:30:02
  •  标签:
  • c++

这是不重复基类代码而给导出类添加 ostream& 运算符 & lt; & lt; 的唯一方法吗? 这些投影是不是应该避免的?

我看不到任何其他方法, 除了定义基类中的某种函数, 它将代表基类中的数据, 定义为 std:: operator< & lt; 可以“ 附加” (如字符串? ), 对导出类做同样的事情( 在导出类流中调用基类流表示函数 。 代表. course. 函数 ) 。

这一问题的理想解决办法是什么?

#include <iostream>

class Base
{
    private: 
        int b_;
    public: 
        Base()
            :
                b_()
        {};

        Base (int b)
            : 
                b_(b)
        {};

        friend std::ostream& operator<<(std::ostream& os, const Base& b);
};

std::ostream& operator<< (std::ostream& os, const Base& b)
{
    os << b.b_; 
    return os;
}

class Derived
:
    public Base
{
    private: 
        int d_;
    public: 
        Derived()
            :
                d_()
        {};

        Derived (int b, int d)
            : 
                Base(b), 
                d_(d)
        {};

        friend std::ostream& operator<<(std::ostream& os, const Derived& b);
};

std::ostream& operator<< (std::ostream& os, const Derived& b)
{
    os << static_cast<const Base&>(b) << " " << b.d_; 
    return os;
}



using namespace std;


int main(int argc, const char *argv[])
{
    Base b(4);

    cout << b << endl;

    Derived d(4,5);

    cout << d << endl;

    return 0;
}
最佳回答

......如果在结果定义不正确但投入基地总是安全的情形下,就应避免铸造。

可以避免明确的表述,因为考虑到衍生的参考衰减为基准参考,所以可以使用默示转换,例如在这种情况下:

std::ostream& operator<< (std::ostream& os, const Derived& b)
{
    const Base& bs = b;
    os << bs << " " << b.d_; 
    return os;
}
问题回答
static_cast<const Base&>(b)

安全,没有不正确之处,因为每个衍生类对象也是基地类对象,可以被当作一个对象对待。

只有在以鲁莽的方式使用时,才有危险,你必须在需要时以正确的方式使用石膏,这是语言标准提供石膏的根本目的。

如果您不喜欢这些投影,您可以让操作员调用一个使用模板方法模式执行的 writeto 函数。

例如:

class Base {
   public:
       std::ostream& writeTo(std::ostream& ostr) const { os << b_; return this->doWriteTo(os); 时 时
   private:
       int b_;

       virtual std::ostream& doWriteTo(std::ostream& ostr) const = 0; // pure virtual
时 时;


class Derived {
    private:
        int d_;
        virtual std::ostream& doWriteTo(std::ostream& ostr) const {return ostr << d_;时 时
时 时;

std::ostream& operator<<(std::ostream& ostr, const Derived& d) {
  return d.writeTo(ostr);

时 时

实际上,使用此模式, 您可以为 operator<< 一劳永逸地写入 base :

std::ostream& operator<<(std::ostream& ostr, const Base& b) {
  return b.writeTo(ostr);

时 时

此模式还排除了使 operator<< 成为 friend 的必要性。

你可以改变这样的事情:

struct Base {
    int b_;
    void print(ostream &o) { o << b_; }
};

struct Derived : Base {
    int d_;
    void print(ostream &o) {
        Base::print(o);
        o <<     << d_;
   }
};

ostream &operator<<(ostream &o, Base &b) {
    b.print(o);
    return o;
}

ostream &operator<<(ostream &o, Derived &d) {
    d.print(o);
    return o;
}

如果 Base 具有虚拟功能(在此示例中它没有),那么 print 可以是其中之一,你可以摆脱 operator< & lt; 的多重超载。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?