Thread: Data members in inheritance

  1. #1
    Registered User
    Join Date
    Apr 2010

    Data members in inheritance

    In my code, I typically use char variables in classes to flag particular properties of that class 1 bit at a time. What I am wondering is, if I use a public inheritance structure with an abstract class, and each derived class uses the variable cFlags1 in its own unique way (i.e. the bits do not mean the same thing), would it make more sense to have the cFlags1 variable declared in each class declaration separately or just once in the base class?

    class Graphics
            virtual AbstractMe()=0;
            char cFlags1;
    For the inherited class Sprite, a way that variable might be used is like this:
    class Sprite : public Graphics
        // cFlags1
        //  0x01 -  visible?
        //  0x02 -  animation initialized?
        //  0x04 -  scaled?
        //  0x08 -  framed?
        //  0x10 -  flash?
    Whereas a Box might use the variable like this:
    class Box : public Graphics
        // cFlags1
        //  0x01 -  visible?
        //  0x02 and 0x04
        //          two bits signify on screen position
    Either way, I need a cFlags1 variable in each class, but the Sprite class is much more complex than the Box class, and does not have the same bitwise properties. Is it alright to keep the cFlags1 declaration in the base class as long as its properties are handled strictly in the derived classes?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Since this is implementation detail specific to each derived class, I think that it should not be in the base class. Consider that you might use a std::bitset<N> instead, where N is specific to each derived class that uses such a method to store properties.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:15 AM
  4. Not able to access private data members
    By smitsky in forum C++ Programming
    Replies: 31
    Last Post: 05-09-2004, 07:06 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM