[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

let baseclass know size of object

.rhavin grobert

11/13/2008 3:51:00 PM

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))' " ...?
6 Answers

Victor Bazarov

11/13/2008 4:24:00 PM

0

..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

Bart van Ingen Schenau

11/14/2008 11:02:00 AM

0

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(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

.rhavin grobert

11/14/2008 5:45:00 PM

0

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(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!

peter koch

11/14/2008 8:29:00 PM

0

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(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

Bob

10/13/2012 12:05:00 PM

0

On 10/12/2012 9:11 PM, Freedom Dude wrote:
>
>
> "Jedi" wrote in message
> news:404f1930-809b-4a83-a714-e44fa616cf2f@rj6g2000pbc.googlegroups.com...
>
> Biden likes to lead from so far behind Obama that he simply laughs and
> smirks his way through the entire national debate and interrupts Mr.
> Ryan more than 80 times. Americans cannot afford to have such
> political clowns who think they know everything in Washington, despite
> the domestic and foreign catastrophe the nation is in as the result of
> their lack of intelligence and leadership. Time for congressional term
> limits do you think?
> ==========================================================
> Biden won that debate hands down. Rightards are infuriated because
> Biden won't let Ryan get away with his lies. He gave a lesson to his
> opponent
> that he will never forget. He shouldn't. Lying will not fly anymore on this
> campaingn. They WILL be called on it.

Substance:
Biden - average
Ryan - average

Specifics:
Biden - average
Ryan - below average

Accuracy:
Biden - poor
Ryan - average

Style/Demeanor/Character:
Biden - loud/obnoxious/phony
Ryan - steady/thoughtful/genuine

Ryan wins.

Jedi

10/14/2012 4:48:00 PM

0

On Oct 13, 2:04 am, Bob <dalnet...@att.net> wrote:
> On 10/12/2012 9:11 PM, Freedom Dude wrote:
>
>
>
>
>
>
>
> > "Jedi"  wrote in message
> >news:404f1930-809b-4a83-a714-e44fa616cf2f@rj6g2000pbc.googlegroups.com...
>
> > Biden likes to lead from so far behind Obama that he simply laughs and
> > smirks his way through the entire national debate and interrupts Mr.
> > Ryan more than 80 times. Americans cannot afford to have such
> > political clowns who think they know everything in Washington, despite
> > the domestic and foreign catastrophe the nation is in as the result of
> > their lack of intelligence and leadership. Time for congressional term
> > limits do you think?
> > ==========================================================
> > Biden won that debate hands down. Rightards are infuriated because
> > Biden won't let Ryan get away with his lies. He gave a lesson to his
> > opponent
> > that he will never forget. He shouldn't. Lying will not fly anymore on this
> > campaingn. They WILL be called on it.
>
> Substance:
> Biden - average
> Ryan - average
>
> Specifics:
> Biden - average
> Ryan - below average
>
> Accuracy:
> Biden - poor
> Ryan - average
>
> Style/Demeanor/Character:
> Biden - loud/obnoxious/phony
> Ryan - steady/thoughtful/genuine
>
> Ryan wins.- Hide quoted text -
>
> - Show quoted text -


Ryan's approach in the past has been bipartisanship. At this time he
is not specific because he favors bipartisan discussions starting from
a workable framework. That frame work is what he stated. That is
honorable.