Solved

Initializing an array

Posted on 2000-03-24
5
195 Views
Last Modified: 2010-04-10
I have a class that hasn't got a default constructor and I want to make an array using new and initialize all its objects with the same value(s).What is the most elegent way to do this?
The current way I use is using a default constructor then calling an Init function using a loop, anything better?
0
Comment
Question by:karouri
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
gurpreets earned 100 total points
ID: 2656359
I will give the answer with the help of an example.

Consider a class for company equipment in which the corporate ID number of the equipment is a mandatory constructor argument.

class EquipmentPiece {
public:
  EquipmentPiece(int IDNumber);
  ...
};

It is not usually possible to create arrays of EquipmentPiece objects.

EquipmentPiece bestPieces[10];           // error! No way to call
                                         // EquipmentPiece ctors
 
 EquipmentPiece *bestPieces =
  new EquipmentPiece[10];                // error! same problem


There are three ways to get around this restriction. A solution for non-heap arrays is to provide the necessary arguments at the point where the array is defined.

int ID1, ID2, ID3, ..., ID10;            // variables to hold
                                         // equipment ID numbers
....

EquipmentPiece bestPieces[] = {          // fine, ctor arguments
  EquipmentPiece(ID1),                   // are provided
  EquipmentPiece(ID2),
  EquipmentPiece(ID3),
  ...,
  EquipmentPiece(ID10)
};

Unfortunately, there is no way to extend this strategy to heap arrays.

A more general approach is to use an array of pointers instead of an array of objects:

typedef EquipmentPiece* PEP;           //PEP is a pointer to an EquipmentPiece

PEP bestPieces[10]                    // fine, no ctors called

PEP *bestPieces = new PEP[10]           // also fine

Each pointer in the array can then be made to point to a different EquipmentPiece object:

for (int i = 0; i < 10; ++i)
  bestPieces[i] = new EquipmentPiece( ID Number );
There are two disadvantages to this approach. First, you have to remember to delete all the objects pointed to by the array. If you forget, you have a resource leak. Second, the total amount of memory you need increases, because you need the space for the pointers as well as the space for the EquipmentPiece objects.

You can avoid the space penalty if you allocate the raw memory for the array, then use "placement new" to construct the EquipmentPiece objects in the memory:
0
 
LVL 22

Expert Comment

by:nietod
ID: 2656421
There is another option.  use a containter class, like vector<>.  Then you can add items to the vector one-at-a-time specifying the values for the constructor each time.   Basically it is like using an array of pointers, but safer because you can't get memory leaks.
0
 
LVL 3

Author Comment

by:karouri
ID: 2657930
Thanks gurpreets,your answer is good,but what is:
>You can avoid the space penalty if you allocate the raw memory for the array, then use "placement new" to construct the EquipmentPiece objects in the memory

0
 
LVL 2

Expert Comment

by:gurpreets
ID: 2660261
What I meant, karouri, was that if u use placement new op to allocate a chunk a memory u don't need to free individual array elements. U can delete the whole space in one go. This way there is lesser probability of not freeing memory.
0
 
LVL 3

Author Comment

by:karouri
ID: 2677769
Thanks gurpreets,and sorry for being late but your answer is excellent..
Thanks too nietod.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now