Thread: POD struct and class multiple inheritance

  1. #1
    Registered User
    Join Date
    Jan 2007

    POD struct and class multiple inheritance

    I have the following struct and classes.

    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:

    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
    Join Date
    Aug 2010
    somewhere in this universe
    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
    New Zealand
    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, 02:37 AM
  2. Multiple inheritance from same base template ambiguity
    By TotalTurd in forum C++ Programming
    Replies: 6
    Last Post: 12-09-2010, 03: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, 07:18 AM