473,765 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1931
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
2093
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 instances, including e.g. heap-allocated member variables). A global overloading of new / delete operators is out of question, because I have to track memory consumption for a specific class, and also because the base class implementation will be...
2
1333
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
3608
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 fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
4
2577
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 TXpQAlloc<MyClass,N> N is a chunking factor (defaults to 10).
6
3565
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 uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
3
19268
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? Is it possible for a class to have const variables? As just declaring the class alone doesn't create any memory, only creation of objects results in memory allocation for the object. Thanks in advance!!!
3
1614
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 the memeory management stuff, and another class, that is only a tile of the image. Let's call the first MemoryImage, and the second ReferenceImage. My code works well for creating non const and const reference images to a non
4
2995
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
2636
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.