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