If you want to initialize any object upon its construction in your constructor, just call its contructor in the class contructor initialization list:
class blah{ public: blah(): name("Hello, world"), stuff(12.0f) {} private: string name; float stuff;};
It just so happens that both string and float have operator= overloaded, so you can also assign to them like this (what you're used to):
class blah{ public: blah() { name = "Hello, world"; stuff = 12.0f; } ...}
Although these are essentially equivalent, they're literally not. In the second example, both name and stuff have been constructed with their default constructors. So, the only way to truly "initialize" those objects as they're constructed is to call their constructors. And, of course, you can only do that when the objects themselves get constructed. But inside the body of the constructor, those objects have already been constructed, which is what initialization lists are for: to give you the chance to construct as they're constructed.
One thing that'll get you into some serious trouble, having come from a Java background, is excessive use of new . As a rule, you rarely have to use new , except for when you'll only know how many items to allocate at runtime. Otherwise, use standard static allocation. In the case of classes, structs, and unions, every member you declare is stack-allocated, so never try to use new to allocate them.
Edited by - merlin9x9 on January 4, 2002 8:26:01 AM