使用(n空)基类以允许在同一容器中存储不同的对象 [英] Using a(n empty) base class to enable storing different objects in the same container

查看:147
本文介绍了使用(n空)基类以允许在同一容器中存储不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个完全不同的对象,Sprite和PhysicsData。

Say I have two different objects that are completely different, Sprite and PhysicsData.

我写了一个空的基类和容器类,可以推送和删除对象/从容器。

I write an empty base class and a container class that can push and remove objects to/from a container.

我创建两个容器来存储两个不同的对象 - Sprite和PhysicsData。
(不同的对象不在同一个类中)

I create two of these containers to store the two different objects - Sprite and PhysicsData. (Different objects aren't together in the same class)

class base
{
};

class ContainerManager
{
public:
    std::vector<base*> list;
    void Push(base *object);
    void Remove(base *object);
};

class PhysicsData : public base
{
    void applyGravity();
};

class Sprite : public base
{
    void Draw();
};

ContainerManager SpriteContainer;
ContainerManager PhysicsDataContainer;

Sprite aSprite;
SpriteContainer.Push(&aSprite);

PhysicsData SomeData;
PhysicsDataContainer.Push(&SomeData);

这是下注的方式吗?

推荐答案

您在C ++中使用Templates,并且仍然担心为一个简单的容器建立一个共同的基类

You're having Templates in C++ and still worrying about having a common base class for a trivial container??

template <class T>
class Container{
  private:
    vector<T> list;
  public:
    void Push(T data);
    T Pop();
};

这篇关于使用(n空)基类以允许在同一容器中存储不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆