Alternative to use array of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    Alternative to use array of objects

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

    Code:
    //defined in x.h
    class interaction
    {
    public :
                    int idnumber;         
    	int conditionnum;
    	int substructurenum;
    	int damage;          
    	int referencenum;    
    	int nextsamehashvalue;
    	int otherreactingss1; 
    	int otherreactingss2; 
    	int firstrelativerate;
    					
    	
    // constructor
    interaction() :
          idnumber(0), conditionnum(0), substructurenum(0), damage(0),
          referencenum(0), nextsamehashvalue(0),
          otherreactingss1(0), otherreactingss2(0), firstrelativerate(0)
         {}
    };
    
    //defined in x.cpp
    interaction ia[11000]; //declared globally
    
    void readinteraction() //declared globally
    {
     for(int i=0; i<11000; ++i)
     {
       //here I will access each object of class 
       //interaction via ia and put some value in
       //each member variables defined in that class
     }
    }
    
    int main()
    {
       readinteraction();
       ofstream ialist(filename); //filename is a file where I will 
                                           //write all the object(i.e ia contents)
       ialist.write((char *) ia,sizeof(ia));  
       ialist.close();
       return 0;
    }
    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.

    Thanks
    Rajeev
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    Working...