473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Class Member Memory Allocation

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
Jan 30 '06 #1
2 1910
TB
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
Jan 30 '06 #2

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

Jan 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Manuel Massing | last post by:
Hi, I was wondering if there is an easy way to keep track of all allocations issued by instances of a specific class and its subclasses (i.e. keep track of memory consumption of the class...
2
by: xuatla | last post by:
Hi, I am not sure whether I describe the subject correctly. What I want to do is for a matrix class --------------------------- class CMatrix { private:
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
4
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
3
by: sam_cit | last post by:
Hi Everyone, When a variable is declared as const, it is initialized along with the declaration, as modification later on is not possible. Having seen that, how about const variables in a class?...
3
by: Torsten Wiebesiek | last post by:
Hi folks, currently I'm writing image classes for easier handling of Intel's IPP library. My idea is to have to different classes. One class that represents a complete image and deals with all...
4
by: James Kanze | last post by:
On Aug 13, 5:32 am, Jack Klein <jackkl...@spamcop.netwrote: While you've made a number of important points: In other words, according to the C standard, any signal
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.