C++ C++;为什么全局静态类不将其信息保存在其他类中?

C++ C++;为什么全局静态类不将其信息保存在其他类中?,c++,class,abstract-class,sfml,C++,Class,Abstract Class,Sfml,好吧,我之前问了一个关于我的问题,但是没有人给出任何建议(可能是因为我无法给出更准确的关注领域),所以我继续测试,并将问题精确到更准确的领域。(如果由于某种原因此处缺少某些内容,该链接将显示我的大部分代码) 因此,基本上,我有一个全局静态类,static sf::Texture PlayerTexture,它保留一个纹理,以便任何和所有精灵都可以指向该纹理作为其纹理。我有一个函数,void SetUp(),然后将所有纹理加载到它们的信息中以供使用。我在int main()的开头使用此函数,以便这

好吧,我之前问了一个关于我的问题,但是没有人给出任何建议(可能是因为我无法给出更准确的关注领域),所以我继续测试,并将问题精确到更准确的领域。(如果由于某种原因此处缺少某些内容,该链接将显示我的大部分代码)

因此,基本上,我有一个全局静态类,
static sf::Texture PlayerTexture
,它保留一个纹理,以便任何和所有精灵都可以指向该纹理作为其纹理。我有一个函数,
void SetUp()
,然后将所有纹理加载到它们的信息中以供使用。我在
int main()
的开头使用此函数,以便这些全局静态纹理都准备好了纹理。但是,当我创建一个新对象时,如果精灵将其纹理设置为
PlayerTexture
,则不会加载纹理,除非我在构造函数中使用
SetUp()
函数

代码:

//rpg.h
静态sf::纹理播放器纹理;
/*
注意:--加载所有纹理以供使用
*/
静态无效设置()
{
//加载纹理。
如果(!PlayerTexture.loadFromFile(“C:/ProgramFiles(x86)/Terentia/Files/Textures/player.png”))
{

std::cout
static
此处表示它仅在翻译单元中可用。包含rpg.h的每个文件都有自己的PlayerTexture副本。
static
表示不同位置的许多内容。当它处于文件级别时,表示它仅在该文件/翻译单元中可用。通常在一个文件中声明它:
sf::Texture PlayerTexture
extern sf::Texture PlayerTexture
其他地方。@clcto谢谢,我花了一点时间才弄明白,但这很好用。@Joe,这可以解释很多问题。我想我只是习惯于在类中声明静态变量。谢谢。
//rpg.h
static sf::Texture PlayerTexture;

/*
    NOTE: --Loads in All Textures for use
*/
static void SetUp()
{
    //Load texture.
    if(!PlayerTexture.loadFromFile("C:/Program Files (x86)/Terentia/Files/Textures/player.png"))
    {
        std::cout<<"Error: texture failed to load..."<<std::endl;
    }
}
//main.cpp
typedef std::shared_ptr<rpg::GameObject> ptrGameObject;

int main()
{
    //Should prepare all textures for use...
    rpg::SetUp();

    sf::Sprite tempSprite;

    //Texture is loaded from rpg::SetUp() and works.
    tempSprite.setTexture(rpg::PlayerTexture);

    //Unless rpg::SetUp()  is called in constructor, the texture will be empty.
    //Even though rpg::SetUp() is called before object is created.
    ptrGameObject player = rpg::CreatePlayer();
}