#include <iostream>
#include <string>
struct Printable abstract
{
friend std::ostream& operator<<(std::ostream& cout, const Printable& obj)
{
obj.print(cout);
return cout;
}
virtual void print(std::ostream& cout) const = 0;
};
struct VirtualBase abstract : public Printable
{
//stuff
};
struct Named abstract : public Printable
{
std::string name;
void print(std::ostream& cout) const
{
cout << "Name: " << name;
}
};
struct DerivedA : public VirtualBase
{
void print(std::ostream& cout) const
{
cout << "DerivedA";
}
};
struct DerivedB : public VirtualBase, public Named
{
void print(std::ostream& cout) const
{
cout << "DerivedB";
dynamic_cast<const Named*>(this)->print(cout);
//Is there a better way to call Named::print?
}
};
的多層由於DerivedB結構設置COUT支持繼承VirtualBase和命名,這兩個的繼承打印,我不能COUT使用DerivedB。在繼承層次的多個層次上進行Printable支持的最佳方式是什麼?另外,在派生類的print中調用Named :: print最簡單的方法是什麼?與繼承
什麼是在你的結構聲明中的'abstract'?這是無效的C++。 – 2012-03-05 23:21:48
@DavidBrown:除非他把它當作宏。 – Linuxios 2012-03-05 23:24:14
@Linux啊是的。如果是這樣的話,您可能希望將它包含在您的代碼示例中,並使其與宏的標準一致,以避免混淆user173342。 – 2012-03-05 23:29:51