Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class Member Memory Allocation

Reply
Thread Tools

Class Member Memory Allocation

 
 
Jon Rea
Guest
Posts: n/a
 
      01-30-2006
Following up on:
> Any reason for creating iv dynamically?

in "Re: Namespace and #Include best practises"

What are the rules for when member data should be created as new memory
with a member pointer vs as just a member?

Cheers,
Jon Rea
 
Reply With Quote
 
 
 
 
TB
Guest
Posts: n/a
 
      01-30-2006
Jon Rea sade:
> Following up on:
> > Any reason for creating iv dynamically?

> in "Re: Namespace and #Include best practises"
>
> What are the rules for when member data should be created as new memory
> with a member pointer vs as just a member?
>


It's a design question, really.

--
TB @ SWEDEN
 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      01-30-2006

Jon Rea wrote:
> Following up on:
> > Any reason for creating iv dynamically?

> in "Re: Namespace and #Include best practises"
>
> What are the rules for when member data should be created as new memory
> with a member pointer vs as just a member?


One case where you would definitely use dynamic allocation, which may
well be helpful in the issue in your previous post about header file
dependencies, is the pimpl idiom.

http://www.gotw.ca/gotw/024.htm

If the member data is an abstract base class pointer and you only know
at run time which particular derived concrete type you need, then
dynamic allocation is necessary there too. The Strategy Pattern is, I
believe, an example.

Bear in mind that with member pointers and dynamic allocation you are
into "Rule of Three" territory. The compiler generated copy
constructor, assignment operator and destructor will probably be wrong
so you will need to write your own. Smart pointers can help, but with
member objects rather than pointers the problem goes away.

It's really a design decision, but I'd start with: Use a member object
when you can and a member pointer with dynamic allocation when you have
to.

Gavin Deane

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Invoking instance before main and memory allocation for static member Alex Vinokur C++ 1 07-24-2003 12:46 PM



Advertisments