C++ Form And Function

Started by
4 comments, last by The Kid 22 years, 7 months ago
I''m building a small game/demo blah blah blah. Ok, what I''m having trouble with is an array of classes. Let me explain. I have a class that I built to handle a certain aspect of the demo I''m working on. There will be many of these classes in the demo. I''m try to make an array of these classes, but I''m having trouble doing so. I''m not sure as to the syntax. When I was not using a contructor for the class, I had no trouble declaring an array of them, but when I used the constructor, Ii was confused by the massive amounts of errors that appeared. Any help would be much appriciated. If I need to explain the situation more, or post source code, please let me know. Thanks in advance. The Kid I don''''t know what the future holds, but I know who holds the future.
I don''t know what the future holds, but I know who holds the future.
Advertisement
Well, I''m not sure how most compilers handle things when you create an aray of instances of classes since I have never tested it out. What I would do in your case, is run the debugger with a breakpoint in the class constructor, and see if it gets called for every class. If it does, then the problem is probably elsewhere, if it doesn''t, then you might want to consider making it an array of pointers to the classes, that way you can create them via new, and be sure that the constructor gets called.

J.W.
In general you use the same syntax as you would use with any type etc.

If your class (eg called MyClass) constructor takes no parameters then something like:

MyClass ArrayOfMyClass[100];

should work fine. You could also do that dynamically like:

MyClass *ArrayOfMyClass=new MyClass[100];

but remember to delete[] ArrayOfMyClass when you are finished with the array if you do so.

If your class constructor has any parameters then you would have to initialise each class instance with those required parameters.

If you''ve already tried all this, then sorry for repeating the obvious to you.
Country: Scotland [not listed in the ''Country'' combobox].
Thanks alot guys. Its good to be able to drop my problems on people that actually know what a syntax is. Thanks again.

The Kid

I don''''t know what the future holds, but I know who holds the future.
I don''t know what the future holds, but I know who holds the future.
You can also make an array that contains objects of various different classes (you really want an array of objects, not classes). Just make an array of (parent-type) and populate it with objects of classes that inherit from (parent-type). Just don''t try to call any methods from the array that don''t exist for all the classes.
To create the arrays:

MyClass array1[100];

and

MyClass* array2 = new MyClass[100];

You will need a default constructor, one that does not take any arguments, to create both of them. Another way to create the second array would be to create an array of pointers to instances of the class. And then fill them in with a for loop or whatever you feel is right in your case.

This could be a solution if you don't want a default constructor.

MyClass* array3[100];

for( int j = 0; i < 100; i++ )
{
array3[j] = new MyClass( "some", "arguments" );
}

Anyway, the reason things went wrong when you addded the constructor is that when you did not have a constructor at all the compiler added a default constructor, that does nothing, for you. This will not be the case if you write your own constructor, so now you need to write your own default constructor.

class MyClass
{
public:
MyClass() {} // Default constructor doing nothing
...
};

Maybe that would be the easy way out for you?

-Benny-

Edited by - benstr on September 25, 2001 10:53:46 AM
-Benny-

This topic is closed to new replies.

Advertisement