Solved

Initializing an array

Posted on 2000-03-24
5
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

740 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