let baseclass know size of object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • .rhavin grobert

    let baseclass know size of object

    guess you have a bouquet of paddingless structs (and your compiler
    already cares for that) that all have one in common: their first
    memeber has to be their size. As fas as i know (am i right?) a
    baseclass get's in it's ctor the _baseclass-size_ when doing a sizeof
    (this), so you cant fill that member in ctor automatically. Lets say
    you want the derived-class way, e.g.:

    typedef unsigned int UINT;
    struct base {
    base(UINT nSize) : m_nSize(nSize) {};
    UINT size() const {return m_nSize;};
    private:
    UINT m_nSize;
    };

    struct d1, public base {
    // .... //
    };

    now what would be the best way to automatically fill the m_nSize
    memeber w/o calling a special init()-member for every derived class or
    doing something like:

    struct d. public base {
    d() : base(sizeof(thi s)) {};
    };


    because depending on the deriviation, ctor d() may or may not have
    some parameters and additional initalisations, so a macro would in the
    end look even more complicated. is there a (perhaps templates) way to
    say:

    "if the struct is derived from 'base', one member of the ctors init-
    list has to be a 'base(sizeof(th is))' " ...?
  • Victor Bazarov

    #2
    Re: let baseclass know size of object

    ..rhavin grobert wrote:
    guess you have a bouquet of paddingless structs (and your compiler
    already cares for that) that all have one in common: their first
    memeber has to be their size. As fas as i know (am i right?) a
    baseclass get's in it's ctor the _baseclass-size_ when doing a sizeof
    (this), so you cant fill that member in ctor automatically. Lets say
    you want the derived-class way, e.g.:
    >
    typedef unsigned int UINT;
    struct base {
    base(UINT nSize) : m_nSize(nSize) {};
    UINT size() const {return m_nSize;};
    private:
    UINT m_nSize;
    };
    >
    struct d1, public base {
    struct d1: public base {
    // .... //
    };
    >
    now what would be the best way to automatically fill the m_nSize
    memeber w/o calling a special init()-member for every derived class or
    doing something like:
    >
    struct d. public base {
    struct d: public base {
    d() : base(sizeof(thi s)) {};
    d() : base(sizeof(*th is)) {}
    };
    >
    >
    because depending on the deriviation, ctor d() may or may not have
    some parameters and additional initalisations, so a macro would in the
    end look even more complicated. is there a (perhaps templates) way to
    say:
    >
    "if the struct is derived from 'base', one member of the ctors init-
    list has to be a 'base(sizeof(th is))' " ...?
    Mmm... I don't know of any. Perhaps your class documentation should say
    that... Besides, what if somebody wants to give your 'size' some value
    determined only during run-time? You shouldn't limit them to providing
    the sizeof(*this) automatically.. .

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • Bart van Ingen Schenau

      #3
      Re: let baseclass know size of object

      On 13 nov, 16:50, ".rhavin grobert" <cl...@yahoo.de wrote:
      guess you have a bouquet of paddingless structs (and your compiler
      already cares for that) that all have one in common: their first
      memeber has to be their size. As fas as i know (am i right?) a
      baseclass get's in it's ctor the _baseclass-size_ when doing a sizeof
      (this), so you cant fill that member in ctor automatically. Lets say
      you want the derived-class way, e.g.:
      >
      <snip>
      is there a (perhaps templates) way to
      say:
      >
      "if the struct is derived from 'base', one member of the ctors init-
      list has to be a 'base(sizeof(th is))' " ...?
      If the restriction that it only works for dynamically-allocated
      structures is not a problem, you could define a class-specific
      operator new() to collect this information.
      Something like this:

      <pseudocode>
      class Base {
      private:
      static std::size_t lastSize;
      public:
      static void* operator new(std::size_t size)
      {
      lastSize = size;
      return ::operator new(size);
      }
      public:
      Base() : mSize(lastSize)
      {
      lastSize = 0; /* reset to avoid undetectable bogus results */
      }
      private:
      unsigned int mSize;
      };
      </pseudocode>

      Bart v Ingen Schenau

      Comment

      • .rhavin grobert

        #4
        Re: let baseclass know size of object

        On 14 Nov., 12:01, Bart van Ingen Schenau
        <Bart.van.Ingen .Sche...@ict.nl wrote:
        On 13 nov, 16:50, ".rhavin grobert" <cl...@yahoo.de wrote:
        >
        guess you have a bouquet of paddingless structs (and your compiler
        already cares for that) that all have one in common: their first
        memeber has to be their size. As fas as i know (am i right?) a
        baseclass get's in it's ctor the _baseclass-size_ when doing a sizeof
        (this), so you cant fill that member in ctor automatically. Lets say
        you want the derived-class way, e.g.:
        >
        <snip>
        is there a (perhaps templates) way to
        say:
        >
        "if the struct is derived from 'base', one member of the ctors init-
        list has to be a 'base(sizeof(th is))' " ...?
        >
        If the restriction that it only works for dynamically-allocated
        structures is not a problem, you could define a class-specific
        operator new() to collect this information.
        Something like this:
        >
        <pseudocode>
        class Base {
        private:
          static std::size_t lastSize;
        public:
          static void* operator new(std::size_t size)
          {
            lastSize = size;
            return ::operator new(size);
          }
        public:
          Base() : mSize(lastSize)
          {
            lastSize = 0; /* reset to avoid undetectable bogus results */
          }
        private:
          unsigned int mSize;};
        >
        </pseudocode>
        >
        Bart v Ingen Schenau
        nice idea, thx, i'll try!

        Comment

        • peter koch

          #5
          Re: let baseclass know size of object

          On 13 Nov., 16:50, ".rhavin grobert" <cl...@yahoo.de wrote:
          guess you have a bouquet of paddingless structs (and your compiler
          already cares for that) that all have one in common: their first
          memeber has to be their size. As fas as i know (am i right?) a
          baseclass get's in it's ctor the _baseclass-size_ when doing a sizeof
          (this), so you cant fill that member in ctor automatically. Lets say
          you want the derived-class way, e.g.:
          >
          typedef unsigned int UINT;
          struct base {
                  base(UINT nSize) : m_nSize(nSize) {};
                  UINT size() const {return m_nSize;};
          private:
                  UINT m_nSize;
          >
          };
          >
          struct d1, public base {
                  // .... //
          >
          };
          >
          now what would be the best way to automatically fill the m_nSize
          memeber w/o calling a special init()-member for every derived class or
          doing something like:
          >
          struct d. public base {
                  d() : base(sizeof(thi s)) {};
          >
          };
          >
          because depending on the deriviation, ctor d() may or may not have
          some parameters and additional initalisations, so a macro would in the
          end look even more complicated. is there a (perhaps templates) way to
          say:
          >
          "if the struct is derived from 'base', one member of the ctors init-
          list has to be a 'base(sizeof(th is))' " ...?
          You could also use the CRTP, although that would mean that you would
          not derive directly from bar, but from an intermediate class:

          template < typename T >
          class BT: public B
          {
          BT(): B(sizeof(T)) {}
          };

          class D: public BT<D>
          {
          ....
          };

          /Peter

          Comment

          Working...