Basic design question

K

keith

Hi,

Can't see this addressed specifically in the FAQ-Lite, so... If a
class needs an internal data structure, which method is commonly held
to be preferable; to embed the structure in the class, or to embed a
pointer in the class, and dynamically allocate the structure in the
ctor (and delete in the dtor)? Or for this straightforward case, does
it not really matter?

Thx
 
A

Anupam Arohi

Hi,

Can't see this addressed specifically in the FAQ-Lite, so... If a
class needs an internal data structure, which method is commonly held
to be preferable; to embed the structure in the class, or to embed a
pointer in the class, and dynamically allocate the structure in the
ctor (and delete in the dtor)? Or for this straightforward case, does
it not really matter?

Thx

Depends a lot on your system design requirements:
1. if internal data structure can be leveraged by other classes and/or
is a complete entity in itself, makes sense to define it independent
of the class and use it as reference / internal object.
2. Dynamic allocation is required only in certain cases where you are
looking for dynamic resource acquisition, if object needs to be
created, and is fairly small in size, use normal stack allocation and
references.
 
V

Victor Bazarov

Can't see this addressed specifically in the FAQ-Lite, so... If a
class needs an internal data structure, which method is commonly held
to be preferable; to embed the structure in the class, or to embed a
pointer in the class, and dynamically allocate the structure in the
ctor (and delete in the dtor)? Or for this straightforward case, does
it not really matter?

Dynamic memory allocation is (a) slow and (b) wasteful. If you can,
keep your internal data plain members. There are other factors that
can sway you toward one solution or the other, but you listed none of
those.

V
 
J

Jim Langston

Hi,

Can't see this addressed specifically in the FAQ-Lite, so... If a
class needs an internal data structure, which method is commonly held
to be preferable; to embed the structure in the class, or to embed a
pointer in the class, and dynamically allocate the structure in the
ctor (and delete in the dtor)? Or for this straightforward case, does
it not really matter?

Only do dynamic memory allocation when you have to. It increases overhead
for both the program and the programmer. When possible embed the instance
in the class.

Some reasons you may have to:
1. You don't know the size of the array (but then it's generally better to
use std::vector anyway
2. The object is huge so you need to use the free store
3. The object uses polymorphism

There are other times I would use the freestore rather than the stack but
usually not associated with embeding in a class. Specfically, when I only
want one instance of a specific object and so the copy constructor and
assignment operators are declared private. To get this object into a vector
or map, I need to use pointers.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top