多态问题:如何检查派生类的类型?

发布于 2024-08-26 09:46:04 字数 1962 浏览 11 评论 0原文

这是我的第一个问题:)

我知道我不应该检查对象类型,而是使用dynamic_cast,但这并不能解决我的问题。

我有一个名为 Extension 的类和名为 IExtendable 和 IInitialized、IUpdatable、ILoadable、IDrawable 的接口(后四个基本相同)。如果Extension实现了IExtendable接口,它就可以用不同的Extension对象来扩展自己。

问题是我想允许实现 IExtendable 的扩展仅与实现与原始扩展相同的接口的扩展扩展。

您可能不理解这种混乱,所以我尝试用代码来解释它:

class IExtendable
{
public:
 IExtendable(void);

 void AddExtension(Extension*);
 void RemoveExtensionByID(unsigned int);

 vector<Extension*>* GetExtensionPtr(){return &extensions;};
private:
 vector<Extension*> extensions;
};

class IUpdatable
{
public:
 IUpdatable(void);
 ~IUpdatable(void);

 virtual void Update();
};

class Extension
{
public:
 Extension(void);
 virtual ~Extension(void);

 void Enable(){enabled=true;};
 void Disable(){enabled=false;};

 unsigned int GetIndex(){return ID;};

private:
 bool enabled;
 unsigned int ID;

 static unsigned int _indexID;
};

现在想象一下我创建这样的扩展的情况:

class MyExtension : public Extension, public IExtendable, public IUpdatable, public IDrawable
{
public:
    MyExtension(void);
    virtual ~MyExtension(void);

    virtual void AddExtension(Extension*);
    virtual void Update();
    virtual void Draw();
};

并且我希望允许此类仅使用实现相同接口(或更少接口)的扩展来扩展自身。例如,我希望它能够采用实现 IUpdatable 的扩展;或 IUpdatable 和 IDrawable 两者;但例如不是实现 ILoadable 的扩展。 我想这样做,因为当例如 Update() 将在实现 IExtendable 和 IUpdateable 的某些扩展上被调用时,它也会在扩展此扩展的这些扩展上被调用。

因此,当我添加一些扩展到扩展实现了 IExtendable 和一些 IUpdatable、ILoadable...我被迫检查要添加的扩展是否也实现了这些接口。所以在 IExtendable::AddExtension(Extension*) 中我需要做这样的事情:

void IExtendable::AddExtension(Extension* pEx)
{
bool ok = true;
// check wheather this extension can take pEx
// do this with every interface
if ((*pEx is IUpdatable) && (*this is_not IUpdatable))
ok = false;

if (ok) this->extensions.push_back(pEx);

}

但是怎么做呢?有什么想法最好的解决方案吗?我不想使用dynamic_cast并查看它是否返回null...谢谢

this is my first question here :)

I know that I should not check for object type but instead use dynamic_cast, but that would not solve my problem.

I have class called Extension and interfaces called IExtendable and IInitializable, IUpdatable, ILoadable, IDrawable (the last four are basicly the same). If Extension implements IExtendable interface, it can extend itself with different Extension objects.

The problem is that I want to allow the Extension which implements IExtendable to extend only with Extension that implements the same interfaces as the original Extension.

You probably don't unerstand that mess so I try to explain it with code:

class IExtendable
{
public:
 IExtendable(void);

 void AddExtension(Extension*);
 void RemoveExtensionByID(unsigned int);

 vector<Extension*>* GetExtensionPtr(){return &extensions;};
private:
 vector<Extension*> extensions;
};

class IUpdatable
{
public:
 IUpdatable(void);
 ~IUpdatable(void);

 virtual void Update();
};

class Extension
{
public:
 Extension(void);
 virtual ~Extension(void);

 void Enable(){enabled=true;};
 void Disable(){enabled=false;};

 unsigned int GetIndex(){return ID;};

private:
 bool enabled;
 unsigned int ID;

 static unsigned int _indexID;
};

Now imagine the case that I create Extension like this:

class MyExtension : public Extension, public IExtendable, public IUpdatable, public IDrawable
{
public:
    MyExtension(void);
    virtual ~MyExtension(void);

    virtual void AddExtension(Extension*);
    virtual void Update();
    virtual void Draw();
};

And I want to allow this class to extend itself only with Extensions that implements the same interfaces (or less). For example I want it to be able to take Extension which implements IUpdatable; or both IUpdatable and IDrawable; but e.g. not Extension which implements ILoadable. I want to do this because when e.g. Update() will be called on some Extension which implements IExtendable and IUpdateable, it will be also called on these Extensions which extends this Extension.

So when I'm adding some Extension to Extension which implements IExtendable and some of the IUpdatable, ILoadable... I'm forced to check if Extension that is going to be add implements these interfaces too. So In the IExtendable::AddExtension(Extension*) I would need to do something like this:

void IExtendable::AddExtension(Extension* pEx)
{
bool ok = true;
// check wheather this extension can take pEx
// do this with every interface
if ((*pEx is IUpdatable) && (*this is_not IUpdatable))
ok = false;

if (ok) this->extensions.push_back(pEx);

}

But how? Any ideas what would be the best solution? I don't want to use dynamic_cast and see if it returns null... thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦醒灬来后我 2024-09-02 09:46:04

你应该重新考虑你的设计。如果您需要这种行为,也许可以引入 IUpdateExtendableIDrawExtendable

当前方法不好的原因是它违反了里氏替换原则。你基本上是在说“这个对象充当 IExtendable,但它实际上不是,除非......”如果一个类实现了一个接口,它确实应该可以通过该接口使用,而无需任何状况。如果不是,这将是一场维护噩梦。

You should rethink your design. If you need this behavior, perhaps introduce IUpdateExtendable and IDrawExtendable.

The reason the current approach is bad is that it violates the Liskov Substitution Principle. You're basically saying "this object acts as an IExtendable, but it really doesn't, unless ...." If a class implements an interface, it really should be usable via that interface without any conditions. If it's not, it is a maintenance nightmare waiting to happen.

爱人如己 2024-09-02 09:46:04

只是一种直觉,但根据您的扩​​展程序正在执行的操作,访问者模式可能会帮助您。您可能还想阅读 装饰器 模式。

Just an intuition, but depending on what your extensions are doing, the visitor-pattern might help you. You may also want to read up on the decorator pattern.

小嗷兮 2024-09-02 09:46:04

我可以将所有这些接口合并到一个类中,这样我会得到如下所示的结果:

class Extension
{
public:
Extension(void);
virtual ~Extension(void);

void AddExtension(Extension* pEx){extensions.push_back(pEx);};

virtual void Initialize()
{
for each (Extension* pEx in extensions) pEx->Initialize();
};

virtual void Load()
{
for each (Extension* pEx in extensions) pEx->Load();
};

virtual void Update()
{
for each (Extension* pEx in extensions) pEx->Update();
};

virtual void Draw()
{
for each (Extension* pEx in extensions) pEx->Draw();
};

private:
vector<Extension*> extensions;
};

但这意味着所有添加的扩展都会调用所有方法。例如,某些添加的扩展中不加载任何内容且不绘制任何内容的空方法将被调用。

I could merge all these interfaces in to one class so I would get something like this:

class Extension
{
public:
Extension(void);
virtual ~Extension(void);

void AddExtension(Extension* pEx){extensions.push_back(pEx);};

virtual void Initialize()
{
for each (Extension* pEx in extensions) pEx->Initialize();
};

virtual void Load()
{
for each (Extension* pEx in extensions) pEx->Load();
};

virtual void Update()
{
for each (Extension* pEx in extensions) pEx->Update();
};

virtual void Draw()
{
for each (Extension* pEx in extensions) pEx->Draw();
};

private:
vector<Extension*> extensions;
};

But this would mean that all of the methods would be called on every added extension. E.g. empty methods in some added extension that doesn't load any content and doesn't draw anything would be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文