R
.rhavin grobert
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))' " ...?
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))' " ...?