Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic array of classes

Status
Not open for further replies.

spirou

Programmer
Oct 22, 2001
22
0
0
FR
Hi, i'm a bit lost by using C++.

I want to store the adresses of each instances of a classe in an array of pointers.

Here is a sample of what i'm trying to do :
Code:
class object{
 ...
};

main()
{
  object **a_objects;
  a_objects = new (* objet)[10];

  objet myobject1();
  a_objects[0] = &myobjet1;

  objet myobject2();
  a_objects[1] = &myobjet2;

  ...
}
This syntax doesn't work, he he it's a bit painful but I'm a noob [tongue]

May someone help me ?


P.s: I hope that there are not too much mistakes in my english. (poor french)
 
a_objects = new (* objet)[10];
a_objects = new object* [10];

-pete
 
hmmm,

another pb :
Code:
class object{
 fct1(){...};
};

main()
{
  object **a_objects;
  a_objects = new objet*[10];

  for( int i; i<10; i++) a_objects[i]=NULL;
  ...
  for( int i; i<10; i++){
    if(
Code:
a_objects[i] != NULL
Code:
 ) /* acces violation ? */
      a_objects[i]->fct1();
  }
  ...
}
 
Sorry sometime I'm a bit stupid.

This code works.
 
Well actually you do have a problem. You need to initiailize the &quot;i&quot; variable in your for loops or it could be anything, for example -200, which might indeed cause an access violation.
Code:
for(int i=0; ....)

Always initialize variables... ALWAYS.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top