POD struct and class multiple inheritance

This is a discussion on POD struct and class multiple inheritance within the C++ Programming forums, part of the General Programming Boards category; I have the following struct and classes. Code: class RefCounted { private: LONG m_nRefCount; public: virtual ~RefCounted(); }; struct Header ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    POD struct and class multiple inheritance

    I have the following struct and classes.


    Code:
    class RefCounted  {
    private: LONG m_nRefCount;
    
    public:  virtual ~RefCounted();
    };
    
    struct Header {
      short us;				
      int     i1;
      int     i2;
    };
    
    struct UnitHeader: public Header {
      BYTE filler[sizeof(ULONG) - (sizeof(UnitHeader) & (sizeof(ULONG) - 1))];
    };
    
    class CHeader : public UnitHeader, public RefCounted {
    };
    RefCounted has a virtual destructor, UnitHeader and Header are POD structs.

    CHeader inherits from UnitHeader and RefCounted.

    Now consider this:

    Code:
    void CHeader::MakeDummy() {
    memset((UnitHeader*)this, 0, sizeof(UnitHeader));
    }
    The 'this' pointer in CHeader is casted to UnitHeader struct and that memory area set to zero.
    It seems kinda hackish to me. Is this safe to do? Can I be sure that the Vtbl is not set to zero or something else goes wrong?

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    if you want to cast that, dynamic_cast would be better than the C style cast.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    This is wrong on so many levels.

    1) Do not ever memset this to zero or use ZeroMemory on this. Even when it truly is a POD type, it's just asking for maintenance issues later. Think of it rather like using a shotgun to kill a fly.
    2) It is not CHeader's responsibility to intialise UnitHeader's members. If you don't want UnitHeader to always initialise its own contents, then provide a 'clear' method inside UnitHeader, for this explicit purpose.
    3) The purpose of this filler array seems to be just to enforce struct alignment. There are better easier and proper ways to do so, such as pragmas supported by your compiler.
    4) If Header is not 4-byte aligned then adding two bytes after it is far less efficient than adding two bytes between the us and i1 members within Header itself. using the appropriate pragma wil put the padding where it is most efficient.
    5) For that matter, simply declaring the members in order of largest to smallest would be a good idea.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct and class multiple inheritance
    By KIBO in forum Windows Programming
    Replies: 0
    Last Post: 01-27-2011, 01:37 AM
  2. Multiple inheritance from same base template ambiguity
    By TotalTurd in forum C++ Programming
    Replies: 6
    Last Post: 12-09-2010, 02:40 AM
  3. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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