C++ "Object"类
C++ "Object" class
在Java中,有一个名为"Object"的泛型类,其中所有类都是的子类。我正在尝试制作一个链表库(用于学校项目),我已经设法使它只适用于一种类型,而不适用于多种类型,所以有类似的东西吗?
编辑:我会发布代码,但我现在没有。
C++中没有通用基类,没有
您可以实现自己的类并从中派生类,但必须保留指针(或智能指针)的集合才能利用多态性。
编辑:在重新分析你的问题后,我必须指出std::list
。
如果你想要一个可以专门处理多种类型的列表,你可以使用模板(std::list
是一个模板):
std::list<classA> a;
std::list<classB> b;
如果你想要一个可以在一个实例中容纳不同类型的列表,你可以采用基类方法:
std::list<Base*> x;
class Object{
protected:
void * Value;
public:
template <class Type>
void operator = (Type Value){
this->Value = (void*)Value;
}
template <>
void operator = <string>(string Value){
this->Value = (void*)Value.c_str();
}
template <class Type>
bool operator == (Type Value2){
return (int)(void*)Value2==(int)(void*)this->Value;
}
template<>
bool operator == <Object> (Object Value2){
return Value2.Value==this->Value;
}
template <class ReturnType>
ReturnType Get(){
return (ReturnType)this->Value;
}
template <>
string Get(){
string str = (const char*)this->Value;
return str;
}
template <>
void* Get(){
return this->Value;
}
void Print(){
cout << (signed)this->Value << endl;
}
};
然后使其成为的子类
相关文章:
- 当覆盖存在时调用基本虚拟"binded to object"函数
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- C++将派生类转换为基类时'object slicing'期间发生的情况
- 如何理解 C++17 标准参考中的 [intro.object]/3? N4659 的
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- "Called Object Type is Not a Function or Pointer" 与 typedef 和类
- C++线程"Call to non-static member function without an object argument"
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- Nan::ObjectWrap to Napi::Object Wrap 无法访问 JavaScript 中的属性
- 现代C++会支持 C# "Object Initalizer"语法吗?
- 指向对象生存期之外的已分配内存的指针是"invalid pointer[s]"还是"pointer[s] to an object"?
- C++ Equivalent of Java Map<String, Object>
- WebKit2GTK+ "window-object-cleared"未清除
- 返回 *&object 时是否允许复制/移动省略?
- QPointer::clear() 是删除其引用的指针,还是"Clears this QPointer object."其他含义?
- 解决方法:'can not be used when making a shared object; recompile with -fPIC'使用Cmake。使用普通的 g++ 工作
- 根据标准,究竟什么是"declaration of an object"
- 将unique_ptr作为<Object>unique_ptr<常量对象返回>