CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    6

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sizeof() on derived class objects

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Posts
    6

    Re: sizeof() on derived class objects

    Quote Originally Posted by laserlight View Post
    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.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sizeof() on derived class objects

    Quote Originally Posted by newHere
    How about this
    If you are going to do that, you should just overload fun instead.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2009
    Posts
    6

    Re: sizeof() on derived class objects

    Size matters :P

    Guess theres no way out. Cheese mates.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sizeof() on derived class objects

    Quote Originally Posted by newHere
    Size matters
    Yoda disagrees
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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
  •  





Click Here to Expand Forum to Full Width

Featured