Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Initialize an Array of Pointers to Objects

Posted on 2005-02-26
8
Medium Priority
?
524 Views
Last Modified: 2010-08-05
Help!

Working on a project that requires the initialization of an array of pointers to objects.

The calling function

      className *arrayPtr = new array [ arraylength];
   
      for (int j = 0; j < arraylength - 1; j++)
            array[j] = NULL;

However, it doesn't want to compile.  

What would be the best way to make this work?

Thx  









0
Comment
Question by:theakst
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13411684
Hi theakst,
    What's the error message?
    Please give exact code (instead of className, array etc.)

Bye
---
Harish
0
 
LVL 3

Expert Comment

by:Dawaffleman
ID: 13411685
i think what you want is:

   className *arrayPtr = new array [ arraylength];
   
     for (int j = 0; j < arraylength - 1; j++)
          *array[j] = NULL;

that is assuming that you want what the array points at to be null
the * dereferences the array's address. with out it you are essentially saying the address of array[j]=0 which is not what you want (i think)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13411687
I think it must be:

     className *arrayPtr = new className[ arraylength];  
 
If not, please send error message...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:Harisha M G
ID: 13411700
Yes, it should be...
className *arrayPtr = new className[ arraylength];
OR
void *arrayPtr = new className[ arraylength];
0
 

Author Comment

by:theakst
ID: 13411755

If I use the *array[j], error msg is as follows:

file.cc: In constructor `className::className()':
file.cc:17: no match for `* obj&' operator

Maybe the error lies in the obj const downstream?

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 total points
ID: 13411770
Ok, since you want an ARRAY OF POINTERS TO AN OBJECT, it must be:

className **arrayPtr = new className *[ arraylength];  
0
 

Author Comment

by:theakst
ID: 13411803
Thanks Jaime!!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13411971
FYI:
With the above method, you still need to initialize each iteration.

Check out the following link for some good safe and efficient methods for creating a two dimensional array:
http://www.tek-tips.com/faqs.cfm?fid=5575

The above link also list a wrapper function that can be use as follow:
className **arrayPtr = Allocate2DArray<className >(X_size, Y_size);

It also has an associated function to clear the memory.

Free2DArray(arrayPtr);
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

580 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