• 15
• 15
• 11
• 9
• 10

A Dynamic Array of Classes? - C++

This topic is 4156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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.

Share on other sites
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...

Share on other sites
Quote:
 Original post by YezuCan 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

Share on other sites
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));

Share on other sites
- 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.

Share on other sites
Why not put it into a compiler and play with it instead of waiting on a reply. You'll learn faster and better from errors (most people do...)

//Not tested:#include <iostream>class Test{public:   Test() { AFunction(0, 0); /* Set a default value if you want... Not needed */ }   ~Test() {}   void AFunction(const int a, const int b);private:   int mA, mB;};void Test::AFunction(const int a, const int b){   mA = a;   mB = b;}int main(){   int num;   std::cin >> num;   Test *test = new Test[num];   if (num > 1)   {       test[num - 1]->AFunction(5, 10);   }   delete [] test; //Remeber to clean up!}

Share on other sites
Wheeze! That helps. Thanks a lot.
And that typedef was unecessary, right. I'm still in transition from C to C++ :D

Share on other sites
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'. :)