如何在C ++中创建静态全局类? (或我应该这样做不同?) [英] How do I make static global classes in C++? (or should I be doing this differently?)

查看:199
本文介绍了如何在C ++中创建静态全局类? (或我应该这样做不同?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是一个类,它包含可以被任何其他类访问的对象和变量,而不需要创建该类的对象或从创建它的类中获取对该对象的引用。我知道在Java中,可以创建一个静态类,它会这样做,但它似乎不像C ++中存在静态类。我该如何做到这一点?

What I want is a class that contains objects and variables that can be accessed by any other class without needing to create an object of that class or get a reference to that object from the class that creates it. I know that in Java a could just create a static class that would do this but it doesn't seem like static classes exist in C++. How can I accomplish this?

让我进一步澄清我正在寻找什么。

说我有一个叫mob的类,在更新时,找出球员,并向他移动。我可以通过在mob的更新函数中引用玩家的引用来做到这一点,或者更好的是在初始化时分配给玩家的引用的mob类中创建一个指针。然而,我真正想要的是能够包括一个头像Global.h,并把播放器放在那里。然后当mob或者程序中的任何其他对象想从播放器获取一些数据,它可以只使用像Global.player.GetPos()

Let me clarify further exactly what I'm looking for.
Say I have a class called "mob" which on update, seeks out the player and moves toward him. I could do this by putting a reference to the player in the update function of the mob, or better yet just create a pointer in the mob class that is assigned to a reference of the player on initialization. However, what I really want is to just be able to include a header like "Global.h" and put the player in there. Then when the mob, or any other object in the program wants to get some data from player, it can just use something like Global.player.GetPos()

或者这是可怕的做法,我现在应该忘记。在这种情况下,请告诉我一个更好的方法来做这件事。

Or maybe this is terrible practice that I should forget about right now. In which case, please tell me a better way to do this whole thing. Thanks.

推荐答案

你可以使用单例模式(你可以在任何地方找到它们,比如Neil Kirk ),或者,如果你不需要面向对象的机制(例如继承或多义性),我个人已经习惯于只使用简单的函数而不是类。

You could ether use a singleton pattern (you can find those alot everywhere, like Neil Kirk mentioned in his comment above), or, if you don't need object-oriented mechanics (like inheritance or polymorphy), I personally grew accustomed to just using simple functions instead of a class.

Mob.hpp

#ifndef MOB_HPP
#define MOB_HPP

namespace Mob {

// "public" functions and variables
extern int someVariable;
extern void moveTowards(Player* player);
extern void retreat(void);

}

#endif

Mob.cpp

#include "Mob.hpp"

// "private" functions and variables
static long _lastUpdate;
static void seekOut(Player* player) {
    //...
}

// "public" function implementation and variables
namespace Mob {
    int someVariable = 0;

    void moveTowards(Player* player) {
        //...
    }

    void retreat(void) {
        //...
    }
}

#include "mob.hpp"

int var = Mob::someVariable;
Mob::moveTowards(player);

标题基本上定义了您通常在您的静态类。这些标记为 extern ,以便它们具有外部链接,并可在其他编译单元中使用。

The header basically defines all the public methods you would normally have in your static class. These are marked as extern so that they have external linkage and can be used in other compilation units.

.cpp文件中有一些额外的东西标记为 static 。当您声明全局函数或变量为 static 时,它们具有内部链接,并且仅在定义它们的单元中可见。

In the .cpp file there some additional things marked as static. When you declare global functions or variables as static, they have internal linkage and are only visible in the unit where they are defined. So in a sense, they are private.

有些人认为这种方法有点倒退,但说实话,在处理单例时, strong>无需需要实际使用它们作为类,或者获得一个指向singleton实例的指针,然后函数只是更简单。虽然有一个缺点:如果你想让怪物不再是一个单身人士,但一个实例对象,你以后必须写一个类。

Some see this approach as being kinda backwards, but to be honest, when dealing with singletons without the need to actually use them as a class somehow, or get a pointer to the singleton instance, then functions are just more simple. One drawback though: If you want the mob not to be a singleton anymore, but an instanceable object, you'd have to write a class later on.

这篇关于如何在C ++中创建静态全局类? (或我应该这样做不同?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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