多类型数据类 c++
Multi Typed Data Class c++
我的问题是:
我需要创建一个支持以下代码的类 A:
class B{
//some composed type
}
A a;
a.setValue(1);
a.setValue(2.2);
a.setValue('c');
a.setValue(B());
问题是我需要 a 实例来保留设置的值,并且只有它。这意味着我需要单个变量来包含数据,或者指向数据,但 A 不知道该类型,将来可以添加更多类型(例如 B)。
重载不能解决这个问题,因为如果类型是 int 而不是 double,值将保留在哪里?模板不能解决这个问题,因为对a.setValue(...)
的调用必须看起来与上面所说的完全一样,没有模板所需的"<"、">"字符。
我还尝试了隐式强制,方法是定义:
class MyObj{
MyObj* theObj;
MyObj(){}
virtual ~MyObj() = 0;//with later empty implementation
setValue(myObj* newVal){theObj = newVal;}
}
class MyInt:MyObj{
myInt(){}
myInt(int theInt){}//for implicit cast from int
}
a.setValue(1);
这失败了,因为编译器可以自动将 int 转换为 myInt,但 setValue 需要 myObj,因此即使强制可以工作,编译器也不会自动进行推导。
我该如何解决这个问题?
谢谢。
以下是
基于 ECS 设计模式可以执行的操作:
struct BaseType {
virtual ~BaseType() {}
};
template <typename T>
struct SpecializedType : public BaseType{
SpecializedType(const T& v) : val(v) {}
~SpecializedType() {}
T val;
};
class A {
std::vector<std::unique_ptr<BaseType>> values;
public:
template <typename T> void add(const T& val)
{
values.push_back(std::make_unique<SpecializedType<T>>(val));
}
template <typename T> T get()
{
for (auto& base : values)
{
auto* ptr = dynamic_cast<SpecializedType<T>*>(base.get());
if (ptr)
return ptr->val;
}
throw std::exception("no value found");
}
}
};
int main(void)
{
A a;
a.add(1.0f);
a.add<std::string>("toto"); // You can specify the type when adding
std::cout << a.get<float>() << std::endl;
std::cout << a.get<std::string>() << std::endl;
std::cout << a.get<int>() << std::endl; // Throws an exception because no int has been added
}
优点:
您可以添加所需的每种类型
C++11/14 内存管理
缺点:
每种类型只能有 1 个值
dynamic_cast
显然,如果需要,您可以改进系统,即通过在值中添加标识符以避免dynamic_cast
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 在运行时处理类型擦除的数据-如何不重新发明轮子