Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:26:49+00:00 2026-05-26T15:26:49+00:00

In C++, I have an Object class, from which a number of other classes

  • 0

In C++, I have an Object class, from which a number of other classes inherit (ie Ball, Paddle, and Wall). Is there a way to have a static member on the Object class that gets inherited as separate static members by each subclass?

Basically, I want Ball, Paddle, and Wall to each have a static member (used to store images for drawing) that they inherited from Object (since an array to store images is a common requirement across all objects), but the static members are separate for each of Ball, Paddle, and Wall. That way every Ball instance has access to the ball images, every Paddle has access to the paddle images, and so on.

If this isn’t possible, what would be a good way to allow similar functionality?

EDIT:

Some more detail on what I’m actually trying to accomplish:

I’m working on a Breakout Clone. I have an Object class, from which all game objects inherit (Ball, Paddle, Block, and Wall). All of these objects have a handful of images that they need access to in order to draw themselves (which is done by Object* to all objects in a std::list and running the virtual method updateAndDraw() on all of them).

Right now, Ball, Paddle, Block, and Wall each have a static sf::Texture (from SFML) array that stores all the images the object would need. It’s static because all instances of each class can share the images, and it would be a waste of memory to load them separately for each instance. The images are loaded and stored in the array the first time each of the respective game objects are constructed.

The image loading and storing functionality is basically the same for all the game objects, so I wanted to move it off of each individual game object and on to Object. My plan was to have a static member on Object that each game object will inherit, and this static member will be independent for each of Ball, Paddle, Block, and Wall, although it’s looking now like this might not work. I would also have a static method on Object called loadImages(string filenames[]), that would take an array of image filenames and load the images into the static image array for the game object that loadImages() was called on. So in the Ball constructor, if I ran loadImages() and passed it some filenames, it would load the images into the static image array for Ball, and the same (independently) for all the other game objects.

After typing this all up, I’m realizing that it might be better to store the images somewhere else, and just give the game objects pointers to those images. This eliminates the need for the images array to be static, and in the constructor I can just call a function on whatever is storing the images and pass it the object type, and it’ll hand me an array of pointers to the images that game object would need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:26:50+00:00Added an answer on May 26, 2026 at 3:26 pm

    No is not possible. You can however redeclare your static variables in each child class.

    class A
    {
    public:
        static int MyStatic;
    };
    
    class B : public A
    {
    public:
        static int MyStatic;
    }
    
    class C : public A
    {
    public:
    }
    

    A::MyStatic is the same as C::MyStatic but B::MyStatic is different.
    I still don’t understand why you should do something like this.

    However, you can also use templates.

    template <typename T>
    struct MyStaticContainer
    {
        static int MyStaticValue;
    }
    
    template <typename T>
    int MyStaticContainer<T>::MyStaticValue = 0;
    

    Then, you can access your field in this way:

    MyStaticContainer<A>::MyStaticValue = 10;
    MyStaticContainer<B>::MyStaticValue = 20;
    MyStaticContainer<C>::MyStaticValue = 30;
    

    These are actually 3 different static fields.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of objects, all from the same class(ColorNum) Each object has
I have a number of classes which are wrapped by other classes to add
I have an NSOperationQueue to which i have added my custom class's objects(inherits from
I have this object: class a { public string Application; public DateTime From, To;
I have a pymzml.run.Reader class from the pymzml package. This is a generator object,
Suppose we have a class. We create an object from the class and when
Example: I have an class that inherits from UIImageView. An object creates an instance
I have an object class called BOM. It has a method, getChildItem() which returns
I have a class which has a well-defined responsibility - to Enrich an object
I have some base class A , and some number of inherited classes: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.