 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jon Rea Guest
|
Posted: Mon Jan 30, 2006 5:00 pm Post subject: Class Member Memory Allocation |
|
|
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
|
Posted: Mon Jan 30, 2006 5:00 pm Post subject: Re: Class Member Memory Allocation |
|
|
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
|
Posted: Mon Jan 30, 2006 5:01 pm Post subject: Re: Class Member Memory Allocation |
|
|
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 |
|
 |
|
|
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
|
|