Skip to main content
GameDev.net gamedev.net
🔒 Locked

A Dynamic Array of Classes? - C++

Started by Yezu Nov 6, 2006 at 8:27 PM 9 replies 35.7k views
Original Post
Yezu
Yezu
Can I make a dynamic array of classes? Meaning: I declare a class, with a non standart constructor;

typedef class 
{
  public:
     
    int x;
    int y;
    my_class(int a, int b);
    ~my_class();

    void a_function(); 
 
   private:
  
   void another_function();

}my_class;


Then during runtime I recieve info on how many such Objects I need. Should the defintion look something like that:

cin >> number;

my_class *array;
array = new my_class[number] //?????


In such a situation how do I call the custom constructor? Is this in any way correct?
_______All your base are belong to us.
jon723
jon723
If I remember correctly, a default constructor is automatically created if you define a constructor that takes arguments. Therefore, the default constructor is automatically called on objects that are dynamically created. As for using a non constant integer in your array, I'm not sure if that is legal.
www.lefthandinteractive.net
Yezu
Yezu
So You suggest that I should forget about the custom constructor, and instead just make a function that fills in the variables, ok. However I still don't know one thing how do I call to such public function?

array_of_my_class->a_function(10,5); // like this?



As to non constant integer in the definition of the array I'm pretty sure it's legal when used with standart types...
_______All your base are belong to us.
janta
janta
Quote:
Original post by Yezu
Can I make a dynamic array of classes?

Meaning:
I declare a class, with a non standart constructor;

*** Source Snippet Removed ***

Then during runtime I recieve info on how many such Objects I need.
Should the defintion look something like that:

*** Source Snippet Removed ***

In such a situation how do I call the custom constructor?
Is this in any way correct?


Hi,

Have you only tried to compile this sample ? On Visual Studio 8 the result is as follows:
typedef class {  public:         int x;    int y;    my_class(int a, int b); //error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    ~my_class(); // error C2523: '<unnamed-tag>::~my_class' : destructor tag mismatch    void a_function();     private:     void another_function();} my_class;


Now if you drop that terrible C-style and write:
class my_class{  public:         int x;    int y;    my_class(int a, int b);    ~my_class();    void a_function();     private:     void another_function();};


you get a:
array = new my_class[number]; //error C2512: 'my_class' : no appropriate default constructor available


That is because you provided a custom constructor for your class which will prevent the compiler from generating a default empty one. Add the following in your class:

my_class(); // default constructor


and everything will compile just fine.

- JA

Agony
Agony
I would suggest std::vector. It is essentially a dynamic array, but hides the nasty details from you, such as allocating and deallocating memory. It also accepts a default value for a class whenever you resize the array, and sets all new members to that default value.
//No need for typedef-ing a class or//  struct.  That is a left-over piece of syntax//  from C that no longer serves any purpose in//  C++ (or the more recent version of C, as far//  as I know.)class my_class{  public:         int x;    int y;    my_class(int a, int b);    ~my_class();    void a_function();     private:     void another_function();};//...  Later#include <vector>//...  In some functioncin >> size;//Make a dynamic array that starts out with//  5 objects, each initialized to (10, 5).//  This initialization is optional.  You//  could leave off the (5, my_class(10, 5))//  part, and you'd simply have an empty array.std::vector<my_class> my_classes(5, my_class(10, 5));//Resize it with more objectsmy_classes.resize(my_classes.size() + 3, my_class(9, 6));
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
jflanglois
jflanglois
- In C++, a dynamic array is spelled std::vector (for all intents and purposes).
- I don't know what typedef class is. I guess you are taking it from C's typedef struct. In C++, you just write class MyClass { ... };
- A default constructor is not generated if you define a non-default constructor.
- Your class had better not depend on x and y being sane if you are going to have them public.

So:

#include <vector>class my_class {  public:         int x;    int y;    my_class(int a, int b) {}    void a_function() {}   private:      void another_function() {}};int main() {  std::vector< my_class > vec;  vec.push_back( my_class( 43, 2 ) ); // add an instance of my_class to vec using the user-defined constructor}



jfl.
Yezu
Yezu
Wheeze! That helps. Thanks a lot.
And that typedef was unecessary, right. I'm still in transition from C to C++ :D
_______All your base are belong to us.
Zahlman
Zahlman
Quote:
Original post by jflanglois
- I don't know what typedef class is. I guess you are taking it from C's typedef struct. In C++, you just write class MyClass { ... };


It is the ungodly abomination that results from:

a) 'typedef struct' will compile, purely for reasons of backward compatibility.
b) 'class' is ok anywhere 'struct' is, because classes and structs are now syntactic sugar for each other (just with different defaults for public/private access).

Never mind that 'class' doesn't exist in the environment that dictates the support for 'typedef struct'. :)
abdulla
abdulla
If it doesn't make sense to have a default constructor in your class, i.e. you can't initialise member variables without more information, then use a std::vector with a smart pointer, so you can individually initialise and assign each array variable, and they will be correctly managed for you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.