C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Class Member Memory Allocation

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jon Rea
Guest





PostPosted: Mon Jan 30, 2006 5:00 pm    Post subject: Class Member Memory Allocation Reply with quote



Following up on:
Quote:
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
Back to top
Gavin Deane
Guest





PostPosted: Mon Jan 30, 2006 5:00 pm    Post subject: Re: Class Member Memory Allocation Reply with quote



Jon Rea wrote:
Quote:
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
Back to top
TB
Guest





PostPosted: Mon Jan 30, 2006 5:01 pm    Post subject: Re: Class Member Memory Allocation Reply with quote



Jon Rea sade:
Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.