简体   繁体   中英

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++. 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. 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:

There is method 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM