let baseclass know size of object

Discussion in 'C++' started by .rhavin grobert, Nov 13, 2008.

  1. 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(this)) {};
    };


    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(this))' " ...?
     
    .rhavin grobert, Nov 13, 2008
    #1
    1. Advertising

  2. On 13 nov, 16:50, ".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.:
    >

    <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(this))' " ...?


    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 ::eek:perator new(size);
    }
    public:
    Base() : mSize(lastSize)
    {
    lastSize = 0; /* reset to avoid undetectable bogus results */
    }
    private:
    unsigned int mSize;
    };
    </pseudocode>

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Nov 14, 2008
    #2
    1. Advertising

  3. On 14 Nov., 12:01, Bart van Ingen Schenau
    <> wrote:
    > On 13 nov, 16:50, ".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.:

    >
    > <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(this))' " ...?

    >
    > 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 ::eek:perator 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!
     
    .rhavin grobert, Nov 14, 2008
    #3
  4. .rhavin grobert

    peter koch Guest

    On 13 Nov., 16:50, ".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 {
    >         // .... //
    >
    > };
    >
    > 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(this)) {};
    >
    > };
    >
    > 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(this))' " ...?


    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
     
    peter koch, Nov 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luigi Donatello Asero

    Let or not let the text float

    Luigi Donatello Asero, Jan 15, 2004, in forum: HTML
    Replies:
    6
    Views:
    473
    Steve R.
    Jan 15, 2004
  2. verbatime
    Replies:
    7
    Views:
    350
    John Harrison
    Mar 4, 2004
  3. Steven T. Hatton
    Replies:
    2
    Views:
    417
    tom_usenet
    Aug 16, 2004
  4. blangela
    Replies:
    8
    Views:
    669
    Erik Wikström
    Sep 26, 2008
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    239
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page