Path: utzoo!dciem!nrcaer!scs!spl1!laidbak!att!alberta!ubc-cs!uw-beaver!cornell!rochester!rutgers!bellcore!faline!thumper!ulysses!andante!alice!ark From: ark@alice.UUCP Newsgroups: comp.lang.c++ Subject: Re: classes with no data members Message-ID: <7943@alice.UUCP> Date: 2 Jun 88 20:38:06 GMT Article-I.D.: alice.7943 References: <464@polari.UUCP> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 45 In article <464@polari.UUCP>, rlb@polari.UUCP writes: > Chapter 7, section 7, for such a small section, contains quite a lot of > facts about the language. The concept of a class that has no data > (as opposed to function) members is a stumbling-block for me. Actually, > I thought I understood it until this section pointed out that you can have > a non-NULL pointer to such a class. I have some questions: > a) What the heck should a pointer to such a class point to? It should point somewhere. Less flippantly, consider a class T with no data members and a class S derived from T. Then a T* may indeed point into the middle of the S that contains it. Such a pointer is completely meaningful. Or consider a class with no data members but with virtual functions. Such a class contains some information even though there's no way to get to it directly. > b) Should the "sizeof" such a class be zero? Maybe, maybe not. It certainly won't be zero if the class has any virtual functions. Even if it doesn't, cfront may pad it with an extra byte to avoid breaking the C compiler. > c) If the "sizeof" such a class is zero and I use "new" to create an > object of that class, should the amount of free store decrease? Maybe, maybe not. It may well be that the allocator imposes an overhead for each object even if the size of the object is zero. > d) The proposed ANSI standard does not guarantee whether its storage > allocators return NULL or not for objects of size zero. Should C++ > guarantee the behavior of the default implementation of "new" when > asked to allocate zero bytes? Probably not, because in practice it may be forced to rely on the underlying C implementation. > e) If I declare an "auto" array of objects of such a class, should it > consume any stack space? If and only if sizeof such an object is non-zero.