?
Solved

Initialize an Array of Pointers to Objects

Posted on 2005-02-26
8
Medium Priority
?
519 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

777 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