By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,426 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,426 IT Pros & Developers. It's quick & easy.

let baseclass know size of object

P: n/a
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))' " ...?
Nov 13 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
..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(this)) {};
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))' " ...?
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
Nov 13 '08 #2

P: n/a
On 13 nov, 16:50, ".rhavin grobert" <cl...@yahoo.dewrote:
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 ::operator new(size);
}
public:
Base() : mSize(lastSize)
{
lastSize = 0; /* reset to avoid undetectable bogus results */
}
private:
unsigned int mSize;
};
</pseudocode>

Bart v Ingen Schenau
Nov 14 '08 #3

P: n/a
On 14 Nov., 12:01, Bart van Ingen Schenau
<Bart.van.Ingen.Sche...@ict.nlwrote:
On 13 nov, 16:50, ".rhavin grobert" <cl...@yahoo.dewrote:
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 ::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!
Nov 14 '08 #4

P: n/a
On 13 Nov., 16:50, ".rhavin grobert" <cl...@yahoo.dewrote:
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
Nov 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.