English 中文(简体)
<<> 运营商和继承类别
原标题:overloading << operators and inherited classes

我先上课,然后上课。 我愿超载这些衍生产品的“设计、设计、”经营者。 对于普通经营者,即+ ,虚拟功能是trick的。 我的理解是,标准公约是宣布

friend ostream& operator<<(ostream& out, MyClass& A);

在我的班子内,然后界定课后的职能。 我先想增加上述定义的虚拟内容,会使其发挥作用,但经过一些想法(以及我的汇编者错误)之后,我认识到,这并不具有意义。

我在测试案件中尝试了不同的打字,所有阶级成员都是公开的。 例如:

class Foo{
 //bla
};

ostream& operator<<(ostream& out, Foo& foo){
  cout << "Foo" << endl;
  return foo;
}

class Bar : public Foo{
 //bla
};

ostream& operator<<(ostream& out, Bar& bar){
  cout << "Bar" << endl;
  return bar;
}

///////////////////////

Bar bar = Bar();
cout << bar << endl; // outputs  Foo , not  Bar  

因此,在某种程度上,这就是“吗morph变坏”——基类操作员加固;加固;被叫作的不是衍生的班级经营者。 在上述例子中,我如何使正确的运营商要求获得衍生产品类别? 更一般地说,如果我的班子有私人成员,我想保护他们,那么我如何在使用朋友的关键词时纠正经营者超负荷工作的做法?

最佳回答

You can use a virtual helper function. Here s a completely untested example, so excuse any syntax mistakes:

virtual ostream& Foo::print(ostream& out) const {
    return out << "Foo";
}

virtual ostream& Bar::print(ostream& out) const {
    return out << "Bar";
}

// If print is public, this doesn t need to be a friend.
ostream& operator<<(ostream& out, const Foo& foo) {
    return foo.print(out);
}

<><>Edit>: 每项建议均得到清理。

问题回答

通常,在基数类别中,你只是采用单一自由朋友功能要求的多变的<条码>。

operator<< a free function that forwards to a virtual 页: 1 Foo。

<http://ideone.com/hbegs”rel=“nofollow> 见行动

现行适当的法典修改后,您的代码行重;无需做任何工作:

ostream& operator<<(ostream& out, Foo& foo) {
  out << "Foo" << endl;  //  out  and not  cout 
  return out;  // returns  out  and not  foo 
}

ostream& operator<<(ostream& out, Bar& bar) {
  out << "Bar" << endl;  //  out  and not  cout 
  return out;  // returns  out  and not  bar 
}

rel=“nofollow”>Demo。 查阅<条码> 私人/代码>的成员,可在所希望的<条码><>/代码>中将此职能作为<条码> 友好/代码>。





相关问题
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?

热门标签