How to get the correct size of inherit class?

Z

Zhou Fan

Here is asserts:
BOOST_CHECK(sizeof(base_config) != sizeof(mine_config));
BOOST_CHECK_EQUAL(sizeof(mine_config), mine_config().size());

Here is implement:

class base_config
{
public:
virtual unsigned size() const { return sizeof(*this); }
};

class mine_config :
public base_config
{
public:
int x;
};

Here is result:
error in "testSize": check sizeof(mine_config) == mine_config().size()
failed [8 != 4]

Why wrong? How to do it?
Thanks a lot.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
R

Rolf Magnus

Zhou said:
Here is asserts:
BOOST_CHECK(sizeof(base_config) != sizeof(mine_config));
BOOST_CHECK_EQUAL(sizeof(mine_config), mine_config().size());

Here is implement:

class base_config
{
public:
virtual unsigned size() const { return sizeof(*this); }
};

class mine_config :
public base_config
{
public:
int x;
};

Here is result:
error in "testSize": check sizeof(mine_config) == mine_config().size()
failed [8 != 4]

Why wrong?

Well, what did you expect? Since base_config::size() is not overriden in
mine_config, that base class's version is called. And in base_config,
*this
is of course of type base_config.
How to do it?

Override size() in every class derived from base_config.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
V

Vijay

See. size of mine_config = size of virtual pointer, as u have declare
function size() as virtual, and size of int x.

And class base_config, having only virtual pointer.
Thats why, size of mine_config = 4+ 4, and size of base_config = 4.

U have to remove either virtual keyword or that Assert to make it error
free

-Vijay Mishra


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
A

Alf P. Steinbach

* Zhou Fan:
Here is asserts:
BOOST_CHECK(sizeof(base_config) != sizeof(mine_config));
BOOST_CHECK_EQUAL(sizeof(mine_config), mine_config().size());

Here is implement:

class base_config
{
public:
virtual unsigned size() const { return sizeof(*this); }
};

class mine_config :
public base_config
{
public:
int x;
};

Here is result:
error in "testSize": check sizeof(mine_config) == mine_config().size()
failed [8 != 4]

Why wrong?

Well, it's actually correct. But if you mean, why doesn't
base_config::size return sizeof(mine_config) when called on a
mine_config object, that's because sizeof is a compile time expression.

How to do it?

Override size in your mine_config class.
Thanks a lot.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top