Phlip said:
You appear to be writing Java. Here's MyClass with a member of type
Matrix:
class MyClass
{
public:
MyClass():
myObject(CreateMatrix())
{}
private:
Matrix myObject;
};
That's uncompiled, but others will check my work.
In C++, objects occupy storage ("memory"), and that storage might in turn
be inside the storage for a containing object. Java puts all objects on
the heap. C++ puts objects on the heap with 'new' and releases them with
'delete'. Otherwise, allocation and deallocation are automatic, based on
the size of an object and its location, in global storage, static storage,
on the stack, or inside another object.
I'm not sure that's the best way to address the fundamental question. How
'bout a general "read up on RAII"? Add to that, learn the distinction
between 'reference semantics' (an unfortunate term in C++; 'indirection
semantic'?) and 'value semantics'.
Some problems, as I see things, with your example are 1) Unless you are
passing parameters to CreateMatrix(), you should (might) be able to rely on
a default constructor. 2) There are potential problems with the
myObject(CreateMatrix()) syntax, such as slicing, or excessive copying.
I'm sure that is not the kind of mechanism you had in mind, but for the
uninitiated, your example might lead to incorrect assumptions.
The use of the member initialization block, is, in my opinion _good_ advise,
and I typically initialize everything with one, whether it's superfluous or
not. Unfortunately, in a tutorial example, using a member initialization
block hides the fact that UDT members (as opposed to pointers to UDT
members) are default initialized.
Actually, the original poster used a very good choice of words in the
subject header (and will be hearing from my copyright lawyer soon

). I
actually proposed that one of the oldtimers write a shortstory called "The
Life and Times of a C++ Object". It's a very important concept to
understand. Stroustrup's discussion of this is pretty good in TC++PL(SE)
with the exception that he's not clear as to which end is up. I.e.,
inheritance forms a tree with the root above, whereas a class instance is
constructed bottom up, from least derived to most derived type.