我问这个,因为即使它似乎工作,我觉得它不应该.目标是让一组对象保持活动,并对它们进行一般访问.这就是我现在所拥有的:
获取基指针访问权限:
struct base { virtual void tick() = 0; }; //ptr access
Run Code Online (Sandbox Code Playgroud)
使用从中继承的不同类型:
struct :public base
{
void tick() { cout << "im type 1" << endl; }
}type1;
struct :public base
{
void tick() { cout << "im type 2" << endl; }
}type2;
Run Code Online (Sandbox Code Playgroud)
然后是一个容器类,应该能够存储任何数量的序列化:
class control
{
struct xtype //struct for organizing objects
{
vector<char>charbuf; //serialized object
}xtype_template;
vector<xtype>xtype_vec;
public:
template<typename T> base* tell_to(T &input) //take object, return (base*)
{
xtype_template.charbuf.resize(sizeof(input));
memcpy(xtype_template.charbuf.data(), (char*)&input, sizeof(input));
xtype_vec.push_back(xtype_template); //push back …Run Code Online (Sandbox Code Playgroud)