C++ 中的动态结构 [英] Dynamic structures in C++

查看:31
本文介绍了C++ 中的动态结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个模拟,其中我有一个使用不同模型的类的对象.这些模型是为类中的某些对象随机选择的,并且也专门为某些对象决定.这些对象相互通信,我在 C++ 中使用结构(又名结构),其中有一些

I am running a simulation in which I have objects of a class which use different models. These models are randomly selected for some objects of the class and specifically decided for some objects too. These objects communicate with each other for which I am using structures (aka struct) in C++ which has some

  1. 标准变量和
  2. 一些额外的变量,这些变量取决于相互通信的对象所具有的模型.

那么,我该怎么做?

提前致谢.

推荐答案

结构或类的所有实例都具有相同的结构.幸运的是,有一些技巧可用于模拟"您尝试执行的操作.

All instances of a structure or class have the same structure. Luckily, there are some tricks that can be used to 'simulate' what you try to do.

第一个技巧(也可以在 C 中使用)是使用联合,例如:

The first trick (which can also be used in C), is to use a union, e.g.:

struct MyStruct
   {
   int field1;
   char field2;
   int type;
   union
      {
      int field3a;
      char field3b;
      double field3c;
      } field3;
   };

在联合中,所有成员在内存中占用相同的空间.作为程序员,你必须小心.你只能从联合中取出你放入的东西.如果你初始化联合的一个成员,但你读了另一个成员,你可能会得到垃圾(除非你想做一些低级的黑客,但不要这样做除非您非常有经验).

In a union, all members take up the same space in memory. As a programmer you have to be careful. You can only get out of the union what you put in. If you initialize one member of a union, but you read another member, you will probable get garbage (unless you want to do some low-level hacks, but don't do this unless you are very experienced).

联合通常与另一个字段(联合之外)一起使用,该字段指示联合中实际使用的成员.您可以将其视为您的条件".

Unions often come together with another field (outside the union) that indicates which member is actually used in the union. You could consider this your 'condition'.

第二个技巧是使用状态"模式(参见 http://en.wikipedia.org/wiki/State_pattern).从外面看,上下文类看起来总是一样的,但在内部,不同的状态可以包含不同种类的信息.

A second trick is use the 'state' pattern (see http://en.wikipedia.org/wiki/State_pattern). From the outside world, the context class looks always the same, but internally, the different states can contain different kinds of information.

状态的一种稍微简化的方法是使用简单的继承,并使用动态转换.根据您的条件",使用不同的子类,并执行动态转换以获取特定信息.

A somewhat simplified approach for state is to use simple inheritance, and to use dynamic casts. Depending on your 'condition', use a different subclass, and perform a dynamic cast to get the specific information.

例如,假设我们有一个 Country 类.有些国家有总统,有些国家有国王,有些国家有皇帝.你可以这样:

E.g., suppose that we have a Country class. Some countries have a president, others have a king, others have an emperor. You could something like this:

class Country
   {
   ...
   };

class Republic : public Country
   {
   public:
      const string &getPresident() const;
      const string &getVicePresident() const;
   };

class Monarchy : public Country
   {
   public:
      const string &getKing() const;
      const string &getQueen() const;
   };

在您的应用程序中,您可以使用指向 Country 的指针,并对需要总统或国王的共和国或君主制进行动态转换.

In your application you could work with pointers to Country, and do a dynamic cast to Republic or Monarchy where the president or king is needed.

使用状态"模式可以轻松地将这个示例转换为一个示例,但我将此作为练习留给您.

This example can be easily transformed into one using the 'state' pattern, but I leave this as an exercise for you.

就我个人而言,我会选择状态模式.我不是动态演员表的忠实粉丝,它们对我来说似乎总是一种黑客.

Personally, I would go for the state pattern. I'm not a big fan of dynamic casts and they always seem to be kind-of-hack for me.

这篇关于C++ 中的动态结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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