On Sep 5, 5:54 am, "deepakvs...@gmail.com" <deepakvs...@gmail.com>
wrote:
On Sep 5, 8:51 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<deepakvs...@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
what is the size of an class with not data members?
ex:
class A {
};
also
sizeof ?
class A{
int f(){
cout<<"Hello";
}
};
Usually 1. Why don't you try it?
It's implementation defined. It's usually 1 on byte addressed
machines, but probably the same as sizeof(int) on a word
addressed machine.
i've tried it... but why is it 1? Is it just for memory allocation so
that no two objects are allocated the same space?
It's principally so that no two objects (of the same type) have
the same address. If you write something like:
A array[2] ;
you are guaranteed that &array[0] != &array[1].
Note that there are certain, very special cases where the
compiler is allowed to allocate less memory than the size of the
object. For example, given:
class A {} ;
class B : public A { int i ;} ;
With many compilers, sizeof( B ) == sizeof( int ), even though
sizeof( A ) + sizeof( int ) is larger. (This is called the
"empty base class" optimization.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34