Life of a class

U

user

Hi,

I am using a matrix library in this way:

MyClass::myFunc()
{
Matrix *p = CreateMatrix( ... );
...
}

MyClass myObject;
myObject.myFunc();

Since I cannot see from the documentation how to deallocate the object,
I want to know if the object is deleted when leaving myFunc(), or when
myObject is deallocated?

Thanks
 
P

Phlip

user said:
MyClass::myFunc()
{
Matrix *p = CreateMatrix( ... );
...
}

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.
 
S

Scott McPhillips [MVP]

user said:
Hi,

I am using a matrix library in this way:

MyClass::myFunc()
{
Matrix *p = CreateMatrix( ... );
...
}

MyClass myObject;
myObject.myFunc();

Since I cannot see from the documentation how to deallocate the object,
I want to know if the object is deleted when leaving myFunc(), or when
myObject is deallocated?

Thanks

Neither leaving the function nor deallocating myObject will deallocate
the object. The documentation (or its authors) are your only hope.
 
S

Steven T. Hatton

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.
 
P

Panjandrum

Scott said:
Neither leaving the function nor deallocating myObject will deallocate
the object.

You don't know that. myObject may own the created Matrix and deallocate
it (a.k.a. RAII).
The documentation (or its authors) are your only hope.

.... or a quick look at the source code ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top