English 中文(简体)
C+++级交流便利
原标题:Facilitating class communication c++
  • 时间:2012-05-24 22:44:09
  •  标签:
  • c++
  • class

我正在写一个游戏, 并且有一个主类叫做 GameManager 。 我希望它尽可能抽象。 在它里面, 它有其它类、 玩家和项目管理器的对象。 想象一下我在播放器中有一个功能, 可以检测玩家是否在某个区域( 检查 x 和 y 值) 。 例如, 如果玩家在那个区域, 如果我想生成一个项目创建Troit () 。 我将如何促进各类之间的沟通?

问题回答

一种可能性是"https://en.wikipedia.org/wiki/serview_pattern" rel=“nofollow”>observer 模式 。在这种模式中,有一个主体保留了观察员名单。当主题状态发生变化时,它通知观察员,他们可以酌情作出反应。在这种情况下,玩家是你的主体,游戏管理员是观察员。当玩家的位置发生变化时,它通知游戏管理员,然后他可以生成一个项目或采取其他行动。

目前我试图这样做的方式是定义一个抽象的基级(dubbbed Commando),该级具有虚拟函数命令(字符串 cmd),并让每个与游戏相关的对象(和对象的管理者)继承该类,并用代码取代命令(),以分析文字字符串(或者在管理者的情况下,分析或截断,并传递到地图中的子目标);这一方法有局限性,但对我而言是有效的。

command.h:
class Commando
{
public:
  virtual int Command(std::string const& cmd) = 0;
};

atom.h:
#include "command.h"

class Atom : public Commando
{
public:
  int Command(std::string const& cmd);
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签