[英]Initializing static class member which is another class object
I am newbie to this forum and my question is probably newbie-like too. 我是这个论坛的新手,我的问题也可能像新手一样。
To be specific: I am working on a simple 2D game in SFML library, lang: C++. 具体来说:我正在SFML库(语言:C ++)中开发一个简单的2D游戏。 There would be an object which presents a brick.
会有一个呈现砖块的物体。 This is probably irrelevant.
这可能无关紧要。 I would like my bricks to look the same on the screen, so i just made only one texture for them.
我希望砖块在屏幕上看起来一样,所以我只为它们制作了一种纹理。 And here is my problem:
这是我的问题:
I just declared an sf::Texture as a member of a brick class. 我刚刚声明了sf :: Texture作为Brick类的成员。 The thing is that the texture is one and I don't want to load it or alloc memory for it every time I create new instance of brick class.
问题是纹理是一个纹理,我不想每次创建砖类的新实例时都为其加载或分配内存。 I would like to create it only once in code and not change it anywhere else.
我只想在代码中创建一次,而不在其他任何地方进行更改。 So I thought I can make it static.
所以我认为我可以使其静止。 Since texture in SFML is also a class I came across kind of mystery for me:
由于SFML中的纹理也是一门课,因此我遇到了种种神秘感:
There is method LoadFromFile(). 有方法LoadFromFile()。 I would like to call it out to load my texture.
我想调用它来加载我的纹理。 How do I call out methods of the class which is static member of another class.
如何调出该类的方法,该方法是另一个类的静态成员。
PS: This is probably the worst description you ever read. PS:这可能是您读过的最糟糕的描述。 The truth is I can't describe anything to anyone.
事实是我无法向任何人描述任何东西。 There is always more talking then facts etc. Hope you understood my issue.
总是比事实多说话。希望您理解我的问题。
I'm not sure if I understand your problem right but answer on your general question "How do I call out methods of the class which is static member of another class" looks like crude code below: 我不确定我是否理解您的问题,但可以回答您的一般问题“如何调用属于另一个类的静态成员的类的方法”,看起来像下面的原始代码:
#include <iostream>
class A
{
public:
void printStr() { std::cout << "This is from A" << std::endl; };
};
class B
{
public:
// Static member declaration.
static A a;
};
// Define a
A B::a;
int main()
{
B::a.printStr();
// or
B b;
b.a.printStr();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.