指向派生类调用函数的基类指针。 [英] Base Class pointers to Derived Classes calling functions.

查看:86
本文介绍了指向派生类调用函数的基类指针。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿。我有一个基类(SPRITE),并且使用这个基类我有

派生了大量的派生类(PERSON,BULLET,MISSILE,

等)。现在,在我的程序中的某个点上,我有一对

指针,其中每个都是指向基类的指针(每个都是一个SPRITE

*)。我知道这些指针中的每一个实际上都指向了一个

派生类,即使指针的类型是SPRITE *,但是

我不知道它指向哪个派生类。我想写一个

的函数,这些函数根据什么来实现不同的取决于两个指​​针所指向的派生类的组合(即我想要

PERSON *和BULLET *的一个功能,

PERSON *和MISSILE *的第二个功能,BULLET *和MISSILE *的三分之一,等等。


如何编写这些函数,以及如何调用它们,以便在给出两个指针的情况下调用

正确的函数?


我希望这很清楚...

解决方案

12月30日凌晨2:03,bgold< bgol ... @ gmail .comwrote:


嘿。我有一个基类(SPRITE),并且使用这个基类我有

派生了大量的派生类(PERSON,BULLET,MISSILE,

等)。现在,在我的程序中的某个点上,我有一对

指针,其中每个都是指向基类的指针(每个都是一个SPRITE

*)。我知道这些指针中的每一个实际上都指向了一个

派生类,即使指针的类型是SPRITE *,但是

我不知道它指向哪个派生类。我想写一个

的函数,这些函数根据什么来实现不同的取决于两个指​​针所指向的派生类的组合(即我想要

PERSON *和BULLET *的一个功能,

PERSON *和MISSILE *的第二个功能,BULLET *和MISSILE *的三分之一,等等。


如何编写这些函数,以及如何调用它们,以便在给出两个指针的情况下调用

正确的函数?


我希望这很清楚...



不确定你想要的是一个重载函数自动

弄清楚基于指针正确重载;如果那是

的情况,那么我不认为用指针是可能的。


然而你可以使用dynamic_cast(甚至typeid)找出指针指向的
然后在代码中进行适当的调用

。虽然这个设计不能很好地扩展,因为你增加了更多的

课程。


感谢和问候

Sonison James


bgold< bg ***** @ gmail.comwrote:


嘿。我有一个基类(SPRITE),并且使用这个基类我有

派生了大量的派生类(PERSON,BULLET,MISSILE,

等)。现在,在我的程序中的某个点上,我有一对

指针,其中每个都是指向基类的指针(每个都是一个SPRITE

*)。我知道这些指针中的每一个实际上都指向了一个

派生类,即使指针的类型是SPRITE *,但是

我不知道它指向哪个派生类。我想写一个

的函数,这些函数根据什么来实现不同的取决于两个指​​针所指向的派生类的组合(即我想要

PERSON *和BULLET *的一个功能,

PERSON *和MISSILE *的第二个功能,BULLET *和MISSILE *的三分之一,等等。


如何编写这些函数,以及如何调用它们,以便在给出两个指针的情况下调用

正确的函数?


我希望这很清楚......



使用访客模式。更好的是,重新设计你的程序,这样你就不会有这样的情况。


维基百科有一篇关于访客模式的好文章,包括样本

代码(在Java中。)检查出来。


12月30日下午3:33,sonison.ja ... @ gmail。 com写道:


12月30日凌晨2:03,bgold< bgol ... @ gmail.comwrote:


嘿。我有一个基类(SPRITE),并且使用这个基类我有

派生了大量的派生类(PERSON,BULLET,MISSILE,

等)。现在,在我的程序中的某个点上,我有一对

指针,其中每个都是指向基类的指针(每个都是一个SPRITE

*)。我知道这些指针中的每一个实际上都指向了一个

派生类,即使指针的类型是SPRITE *,但是

我不知道它指向哪个派生类。我想写一个

的函数,这些函数根据什么来实现不同的取决于两个指​​针所指向的派生类的组合(即我想要

PERSON *和BULLET *的一个功能,

PERSON *和MISSILE *的第二个功能,BULLET *和MISSILE *的第三个功能,等等。


如何编写这些函数,以及如何调用它们,以便调用

正确函数两个指针?


我希望这很清楚...



不确定你想要的是什么是一个重载函数自动

根据指针找出正确的重载;如果那是

的情况,那么我不认为用指针是可能的。


然而你可以使用dynamic_cast(甚至typeid)找出指针指向的
然后在代码中进行适当的调用

。虽然这个设计不能很好地扩展,因为你增加了更多的

课程。


感谢和问候

Sonison James



dynamic_cast仅适用于具有虚函数的类,

typeid运算符将

更合适......


Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don''t know which derived class it points to. I would like to write a
set of functions that do something different depending on what
combination of derived classes the two pointers point to (i.e. I want
one function for a PERSON * and BULLET *, a second function for a
PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

How can I write these functions, and how do I call them, so that the
correct function is called given the two pointers?

I hope this is clear...

解决方案

On Dec 30, 2:03 am, bgold <bgol...@gmail.comwrote:

Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don''t know which derived class it points to. I would like to write a
set of functions that do something different depending on what
combination of derived classes the two pointers point to (i.e. I want
one function for a PERSON * and BULLET *, a second function for a
PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

How can I write these functions, and how do I call them, so that the
correct function is called given the two pointers?

I hope this is clear...

Not sure if what you want is an overloaded function to automatically
figure out the correct overload based on the pointer; if that''s the
case then I don''t think it would be possible with pointers.

You could however, use dynamic_cast(or even typeid) to figure out what
the pointer is pointing to and then make the appropriate set of calls
in the code. Though this design would not scale well as you add more
classes.

Thanks and regards
Sonison James


bgold <bg*****@gmail.comwrote:

Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don''t know which derived class it points to. I would like to write a
set of functions that do something different depending on what
combination of derived classes the two pointers point to (i.e. I want
one function for a PERSON * and BULLET *, a second function for a
PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

How can I write these functions, and how do I call them, so that the
correct function is called given the two pointers?

I hope this is clear...

Use the Visitor pattern. Better yet, redesign your program so you don''t
have such a situation.

Wikipedia has a nice article on the Visitor Pattern, including sample
code (in Java.) Check it out.


On Dec 30, 3:33 pm, sonison.ja...@gmail.com wrote:

On Dec 30, 2:03 am, bgold <bgol...@gmail.comwrote:

Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don''t know which derived class it points to. I would like to write a
set of functions that do something different depending on what
combination of derived classes the two pointers point to (i.e. I want
one function for a PERSON * and BULLET *, a second function for a
PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

How can I write these functions, and how do I call them, so that the
correct function is called given the two pointers?

I hope this is clear...


Not sure if what you want is an overloaded function to automatically
figure out the correct overload based on the pointer; if that''s the
case then I don''t think it would be possible with pointers.

You could however, use dynamic_cast(or even typeid) to figure out what
the pointer is pointing to and then make the appropriate set of calls
in the code. Though this design would not scale well as you add more
classes.

Thanks and regards
Sonison James

dynamic_cast would work only with classes having virtual functions,
typeid operator would
be more appropriate...


这篇关于指向派生类调用函数的基类指针。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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