[英]C++, finding parent and calling it's method?
I'd like to say that I have looked for a solution to this issue here but just could not find exactly what I was looking for. 我想说的是,我在这里已经找到了解决该问题的方法,但找不到确切的答案。 There may be a simple solution to my issue.
解决我的问题可能很简单。
So take this example: 举个例子:
class Component
{
public:
virtual ~Component() {} // Make Polymorphic;
void Update() {};
};
class UserMadeComponent : public Component
{
public:
void Update() {};
};
int main()
{
std::vector<Component*> _components;
UserMadeComponent* UsersComponent = new UserMadeComponent;
_components.push_back(UsersComponent);
while (true)
{
for (auto _comp = _components.begin(); _comp != _components.end(); _comp++)
{
(*_comp)->Update();
}
}
}
Now the programmer, using this framework, can create any type of component they want. 现在,程序员可以使用此框架创建所需的任何类型的组件。 And they will add this to the component vector.
他们会将其添加到分量向量。 I want to be able to get the type of component and call its Update method.
我希望能够获取组件的类型并调用其Update方法。 Here is a little diagram of what I am trying to accomplish:
这是我要完成的工作的简要图表:
So the goal is to call the "parents" update method. 因此,目标是调用“父母”更新方法。 However, the user can create any type of component they'd like.
但是,用户可以创建他们想要的任何类型的组件。 I can't assume the type of any component inside this array.
我不能假设此数组内任何组件的类型。 I hope this makes sense
我希望这是有道理的
//EDIT// //编辑//
Thanks for people helping me to understand the virtual method calling. 感谢您帮助我理解虚拟方法调用的人们。 But how would I know what that class actually is.
但是我怎么知道那个班级实际上是什么。 For example, the user has created a component called "Transform".
例如,用户创建了一个名为“ Transform”的组件。 I only want to allow one Transform instance in the component array.
我只想在组件数组中允许一个Transform实例。 But obviously if I only know its a component pointer, I can't check for this occurrence.
但是很显然,如果我只知道它的组件指针,就无法检查这种情况。
It's hard to explain so here is another image: 很难解释,所以这里是另一张图片:
You're doing it backwards. 您正在倒退。 Instead, create a virtual method called
do_update
that your derived classes override. 而是创建一个名为
do_update
的虚拟方法,您的派生类将覆盖该方法。 The parent class's update
method should call the virtual do_update
method, which will dispatch properly. 父类的
update
方法应调用虚拟do_update
方法,该方法将正确分派。
This is how the Standard Library does it... 这就是标准库的工作方式...
Use a virtual method. 使用虚拟方法。
Parent: 上级:
virtual void Update();
If you want the Component class to be "pure virtual," meaning you have to subclass it and it just represents an abstraction/interface, you can do: 如果您希望Component类是“纯虚拟的”,这意味着您必须对其进行子类化,并且它仅表示抽象/接口,则可以执行以下操作:
virtual void Update() = 0;
This will give you an error if you try to instantiate a Component object, you create subclass objects only which implement all the pure virtual methods. 如果尝试实例化Component对象,创建仅实现所有纯虚方法的子类对象,这将给您带来错误。
Edit: 编辑:
The below code checks if x is a Transform. 以下代码检查x是否为Transform。 However, doing this means that you probably have a design flaw.
但是,这样做意味着您可能存在设计缺陷。 Why not make a semantically aware container that stops a user from adding two transforms instead?
为什么不建立一个语义上可以阻止用户添加两个转换的容器呢?
dynamic_cast<Transform*>(x) == nullptr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.