On 8 Apr 2003 at 5:47, Steven T. Hatton wrote:
From: "Steven T. Hatton"
The reason I asked the question is that I'm not sure that every instance of a class will have the same size. If they don't, then how would the result of incrementing a pointer (ptr++ ) be calculated?
Each instance of a class -is- the same size, and the compiler handles the low level details of how to increment a pointer, unless you have a pointer to a void, in which case the increment op doesn't work. If you try to put data into your class definition which does not have a fixed size, the compiler will turn nasty and refuse to compile it :) As far as your other question goes, a class constructor is called in two cases: 1. When a variable of the class type is declared - ie: MyClass my_class1; // calls constructor with no parameters MyClass my_class2(params); // calls constructor with parameters 2. When a object of the class type is created with new - ie: MyClass *my_class_ptr1 = new MyClass; MyClass *my_class_ptr2 = new MyClass(params); The classes destructor is called in the following circumstances: 1. When a variable of the class type goes out of scope - which usually means at the end of the block in which it was declared. For most automatic variables that would be when the function exits, but you could, for instance, declare a variable inside a loop, and that would go out of scope when the loop exits. For static variables this is when the program exits. Note that static variable are created before the program enters main(). You have no control over the order in which statics are constructed and destroyed, except that the standard guarantees that they will be destroyed in the reverse order to which they were constructed! 2. When objects are destroyed using delete. Note that if you created an array of objects eg: MyClass *my_class_ptr = new MyClass[MAX_MYCLASS]; then you must destroy the array with delete [] eg: delete [] my_class_ptr; The purpose of constructors is so that you can write initialisation code for your classes and be sure that it will automatically be called when you create an object of that class. Destructors perform guaranteed clean up when you destroy an object. [lotsa stuff snipped here...] Hope this helps alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html