Yes, my statement doesn't really make a lot of sense now that I think about it. What matters is not how the class is defined, but rather how objects of that type are instantiated. A class definition is saved in the compiler's memory space during compilation. Non-inline functions certainly do take up space in your
On Mon, 7 Apr 2003 00:08:54 -0500
"Steven T. Hatton"
I'm still fumbling about with some of these notions. The /new/ operator returns a pointer to an object of the class upon which it was called. If I simply declare a variable of that type, it seems to be created, and the default constructor seems to be called to initialize its members. But it would seem this is not synonymous with an implicit invocation of new. For one thing, the object is accessible through a variable name rather than a pointer reference.
Now if I do something like
void doThis() { MyClass mc; mc.myMethod(); }
I implicitly create an instance of MyClass and set the variable mc to hold that object. If, in turn, MyClass has a member variable of some object type, that is created in a similar manner. I assume this member object is destroyed when its owner is destroyed. Correct? In this case. mc is allocated as a local variable. The compiler may allocate some components of the class on the heap, or it may mmap them. But, they will behave as if they were local variables. When you return from doThis(), the storage for mc will be returned back to the system. void doThis() { MyClass *mc = new MyClass; mc->myMethod(); } In this case, the storage for mc will be allocated in a persistent mannter, classically on the heap, but the compiler may mmap it. When you return from doThis, the variable mc (eg. the pointer) goes out of scope, and you have a memory leak.
One ofthe important things to remember, is that variables within the
class must be explicitly initialized, either from within the
constructors (by default or explicitly), or explicitly by you.
example:
string foo("a string"); // the string, foo is explicitly initialized
// by you through the constuctor.
string *foo = new string("a string"); // same as above.
string foo; // The default initialization will create the default
// empty string for the string class.
When you design a class, you should make sure that all the variables in
the class are initialized when the constructor is called. Otherwise, you
could have some difficulties later.
--
Jerry Feldman