473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get the correct size of inherit class?

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

Mar 23 '06 #1
4 1552
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! ]

Mar 23 '06 #2
Zhou Fan wrote:
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! ]

Mar 23 '06 #3
* 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! ]

Mar 23 '06 #4
Zhou Fan wrote:
virtual unsigned size() const { return sizeof(*this); }
Why wrong? How to do it?


What is the type of 'this' at that particular moment?

--
ruurd

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

Mar 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lasse Skyum | last post by:
I'm wondering if there is any way to do run-time checking for the size of class-instances? The only idea I've come up with is overloading operator new and saving the size passes to it on the...
3
by: ThazKool | last post by:
Is there anyway to write a class or struct that has no storage. It only operates on a reference to an already existing type. This is actually an extension to another thread. The thread went a...
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
15
by: srampally | last post by:
The following code works in firefox. But IE displays the hyperlink at font-size=13 rather than font-size=10. Why? If its a known IE bug, how should I fix it? I always want the hyperlinks to take...
7
by: xformer | last post by:
Hello everybody, today I was working on a web site when I found a strange effect. Take the following html document: ----begin---- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...
30
by: Takehiko Abe | last post by:
I have a <pelement with <ttinside: ;;; <p>A paragraph contains <tt>tt element</tt>.</p> I would like to set the font-size of the TT to the same as the containing <p>. This does not seem to...
1
by: kang jia | last post by:
hi currently i am editing signup page, when user enter deupicated NRIC and click signup, they will go to do_signuppage and read the error message and then after 5 seconds, they will be redirected...
1
by: kang jia | last post by:
hi when user entered particulars in signuppage and click" signup" button, i will direct them to do_signup.php. if say the NRIC is dupicate in datebase, i will redirect them back to signup page...
4
by: Tony | last post by:
Hello! If I have an interface Ia and Ib in this way interface Ia : Ib Here we say that interface Ia implements interface Ib If I have a base class called Base and a derived class called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.