C ++“对象”类
在Java中,有一个名为“Object”的generics类,其中所有类都是其子类。 我正在尝试创建一个链表库(用于学校项目),我已经设法让它只适用于一种类型,但不是多种类型,所以有什么类似的吗?
编辑:我会发布代码,但我现在没有它。
在C ++中没有通用的基类,没有。
您可以实现自己的类并从中派生类,但是您必须保留指针(或智能指针)的集合以利用多态性。
编辑:重新分析您的问题后,我必须指出std::list
。
如果你想要一个可以专注于多种类型的列表,你可以使用模板(而std::list
是一个模板):
std::list a; std::list b;
如果您想要一个可以在单个实例中包含不同类型的列表,则采用基类方法:
std::list x;
class Object{ protected: void * Value; public: template void operator = (Type Value){ this->Value = (void*)Value; } template <> void operator = (string Value){ this->Value = (void*)Value.c_str(); } template bool operator == (Type Value2){ return (int)(void*)Value2==(int)(void*)this->Value; } template<> bool operator ==
然后创建它的子类