|
-
May 16th, 2009, 12:31 PM
#1
sizeof() on derived class objects
Hi, here is the code
Code:
struct Base {
int mem1;
};
struct A : Base {
int mem2;
};
struct B : Base {
int mem3[10];
};
void fun(Base& base) {
cout << sizeof(base) << " ";
};
void main() {
Base base; A a; B b;
cout << sizeof(base) << " " << sizeof(a) << " " << sizeof(b) << endl;
fun(base); fun(a); fun(b);
};
the sizeof in main() worked well, but not after the object is passed into the function fun().
actually, I am trying to write a fun() that can take the base class (Base) object or any derived classes (A, B) objects and prints the correct size of the pass in object, which I failed in the above code.
Any help? Thank you very much!
-
May 16th, 2009, 12:39 PM
#2
Re: sizeof() on derived class objects
 Originally Posted by newHere
actually, I am trying to write a fun() that can take the base class (Base) object or any derived classes (A, B) objects and prints the correct size of the pass in object, which I failed in the above code.
sizeof is an operator whose result is determined at compile time. The object that the base reference refers to can vary at run time. As such, you are asking for the impossible (except in special cases, or possibly by overloading, which may not be quite what you want).
By the way, get rid of the semi-colons after your function definitions. Note that the global main function should return an int, not void.
-
May 16th, 2009, 01:01 PM
#3
Re: sizeof() on derived class objects
 Originally Posted by laserlight
sizeof is an operator whose result is determined at compile time. The object that the base reference refers to can vary at run time. As such, you are asking for the impossible (except in special cases, or possibly by overloading, which may not be quite what you want).
Thanks! How about this
Code:
void fun(Base& base) {
A* a = dynamic_cast<A*>(&base);
if (a) {
cout << sizeof(*a);
} else {
B* b = dynamic_cast<B*>(&base);
if (b) {
cout << sizeof(*b);
} else {
cout << sizeof(base);
}
}
}
I had to make the base class virtual and this added an extra 4 bytes to my classes, which is not something that I want.
-
May 16th, 2009, 01:12 PM
#4
Re: sizeof() on derived class objects
Why do you care about the size of your object? I've been doing this a long time and that's never come up for me.
-
May 16th, 2009, 01:14 PM
#5
Re: sizeof() on derived class objects
it's not very maintainable - everytime you add a derived class you need to remember this function and add in another else if
-
May 16th, 2009, 01:18 PM
#6
Re: sizeof() on derived class objects
 Originally Posted by newHere
How about this
If you are going to do that, you should just overload fun instead.
 Originally Posted by GCDEF
Why do you care about the size of your object? I've been doing this a long time and that's never come up for me.
With few exceptions, same here.
-
May 16th, 2009, 01:29 PM
#7
Re: sizeof() on derived class objects
Size matters :P
Guess theres no way out. Cheese mates.
-
May 16th, 2009, 01:31 PM
#8
Re: sizeof() on derived class objects
 Originally Posted by newHere
Size matters
Yoda disagrees
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|