Original Post
Why does the compiler shows some size even if we don't have any members in the class?
#include <iostream>using namespace std;class CFoo{ public: CFoo( void ) { } virtual ~CFoo( void ) { }} // end class CFoovoid main( void ){ cout << "The size of CFoo is " << sizeof( CFoo ) << "." << endl;} // end mainQuote:
Original post by rpg_code_master
The reason there is some size is because the actual methods themselves take up a little bit of memory. That is where the code inside of them is stored.
Quote:virtual functions do. The class needs a vtable to handle virtual functions, which is the size of a pointer (I think).
Original post by ProgramMax
I don't think member functions increase the size of your class.
They are just like global, non-member functions. The only difference is that there is a hidden parameter that gets passed into the function. Wether you know it or not the first parameter is the 'this' parameter.
If your empty class has a size it is likely due to some sort of padding. Or something else...but not functions.
class CFoo{ // Empty class};void SomeFunc(CFoo* pFoo){ // Do something with pFoo}// Calling the function:CFoo theFoo;SomeFunc(&theFoo); // <-- What is the address of the object if it has no size?Quote:
Original post by TEUTON
Why does the compiler shows some size even if we don't have any members in the class?
Quote:
ISO14882(C++ standard) section 9.3
Complete objects and member subobjects shall have nonzero size
Quote:
Original post by Konfusius
BTW, (sorry can't resist :P) there has never been a void main().
And I am curious to know what an explicit void parameter should communicate...
#include <conio.h>#include <iostream>using namespace std;int main( void ){ cout << "hello!"; while ( !_kbhit( ) ) ; return 0;}int main()int main(int argc, char* argv[])Quote:
Original post by Konfusius
Yes it works (on some compilers), but it's not valid C++ nor C.
Quote:
Original post by rpg_code_master Quote:
Original post by Konfusius
Yes it works (on some compilers), but it's not valid C++ nor C.
I agree with that. I guess I should change [smile]
Quote:
Original post by rpg_code_master Quote:
Original post by Konfusius
BTW, (sorry can't resist :P) there has never been a void main().
And I am curious to know what an explicit void parameter should communicate...
It works, I use it all the time.
I just double tested on Visual Studio 2005 here at work, and it runs fine.
Quote:
ISO14882(C++ standard) section 3.6.1.2
An implementation shall not define the main function. The function shall not be overloaded. It shall have a return type of int but otherwise it's type is implementation defined. All implementations shall allow both of the following definitions of main:
int main() { /*...*/ }
int main(int argc, char* argv[]) { /*...*/ }
Quote:It's an old C thing. In C, before the C99 (Or Csomething) standard, any function not taking any arguments had to be declared as return_type function_name(void).
Original post by Konfusius
And why the extra (void) ? Just curious...
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more