Data members in inheritance

This is a discussion on Data members in inheritance within the C++ Programming forums, part of the General Programming Boards category; In my code, I typically use char variables in classes to flag particular properties of that class 1 bit at ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    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?

    Code:
    class Graphics
        {
        public:
            virtual AbstractMe()=0;
    
        private:
            char cFlags1;
        };
    For the inherited class Sprite, a way that variable might be used is like this:
    Code:
    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:
    Code:
    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
    Location
    Singapore
    Posts
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:23 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07: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, 08:06 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21