470,850 Members | 1,344 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,850 developers. It's quick & easy.

Alternative to use array of objects

180 100+
Hello everybody,
In the following code, please suggest me to use alternative to array of objects.

Expand|Select|Wrap|Line Numbers
  1. //defined in x.h
  2. class interaction
  3. {
  4. public :
  5.                 int idnumber;         
  6.     int conditionnum;
  7.     int substructurenum;
  8.     int damage;          
  9.     int referencenum;    
  10.     int nextsamehashvalue;
  11.     int otherreactingss1; 
  12.     int otherreactingss2; 
  13.     int firstrelativerate;
  16. // constructor
  17. interaction() :
  18.       idnumber(0), conditionnum(0), substructurenum(0), damage(0),
  19.       referencenum(0), nextsamehashvalue(0),
  20.       otherreactingss1(0), otherreactingss2(0), firstrelativerate(0)
  21.      {}
  22. };
  24. //defined in x.cpp
  25. interaction ia[11000]; //declared globally
  27. void readinteraction() //declared globally
  28. {
  29.  for(int i=0; i<11000; ++i)
  30.  {
  31.    //here I will access each object of class 
  32.    //interaction via ia and put some value in
  33.    //each member variables defined in that class
  34.  }
  35. }
  37. int main()
  38. {
  39.    readinteraction();
  40.    ofstream ialist(filename); //filename is a file where I will 
  41.                                        //write all the object(i.e ia contents)
  42.    ialist.write((char *) ia,sizeof(ia));  
  43.    ialist.close();
  44.    return 0;
  45. }
I dont want to use interaction ia[11000] in the above program
means I dont want to specify fixed object. I even dont want
interaction* ia[11000]

Is there any other solution?

Hi, Banfa!!!
I'm not able to solve my previous query.
I'm still looking any suggestion from you.

Sep 7 '06 #1
1 6093
9,065 Expert Mod 8TB
This really depends on what you are using the data for. If you are always going to have 11000 of these objects in use then there really is not harm in declaring the array statically. On the other hand if the number required is variable then you may be better of allocating them.

I wouldn't statically allocate an array of pointers though

interaction* ia[11000]; // <-- I would do this

Better to have a single pointer and allocate an array to it

interaction* ia;

ia = new interaction[11000];

but only if the 11000 is not constant.
Sep 7 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by HikksNotAtHome | last post: by
3 posts views Thread by McKirahan | last post: by
43 posts views Thread by Mountain Bikn' Guy | last post: by
2 posts views Thread by Tristan | last post: by
9 posts views Thread by John A Grandy | last post: by
21 posts views Thread by yeti349 | last post: by
10 posts views Thread by pcnerd | last post: by
7 posts views Thread by heddy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.