Click to See Complete Forum and Search --> : new to C++ need some help with dynamic array


Ennio
February 20th, 2008, 10:17 AM
I'm trying to make an Dynamic Array of an Object.

but the first step I want to make sure I have everything correct creating my Dynamic Array.

Here is what I have for my .h file.


class course{
Public:
course(int maxSize = 1024);
~course();
Private:
int mySize;
int maxCapacity;
string * myArray;


And here is what I have on my .cpp file

(All the include)...
course::course(int maxSize):mySize(0), myCapacity(maxSize){
myArray = new(nothrow) string[maxSize];
}
course::~course(){
delete [] myArray;
}


is that correct? I still need to create the copy constructor too I'm working on that now.

but what I want to know is. Once I have my Dynamic Array how can I make it an array of another class that I have.
}

Paul McKenzie
February 20th, 2008, 10:29 AM
I'm trying to make an Dynamic Array of an Object.If this is not a classroom exercise, I suggest you use std::vector.

but the first step I want to make sure I have everything correct creating my Dynamic Array.

Here is what I have for my .h file.


class course{
Public:
course();
~course();
Private:
int maxSize;
string * myArray;

That cannot be your real code. There is no such keyword as Private or Public in C++. The keywords are private and public.

Next time, please post your real code, i.e. copy and paste it directly from your code editor into the message window so as to avoid any typographical errors from interfering with the problem/solution.

And here is what I have on my .cpp file

(All the include)...
course::course(){
myArray = new(nothrow) string[10];
}
course::~course(){
delete [] myArray;
}

is that correct?I guess so, not knowing what else is in your class.
I still need to create the copy constructor too I'm working on that now.You also need an assignment operator. However, usage of std::vector requires you to not write any copy constructor or assignment operator:

#include <vector>
#include <string>
class course
{
public:
course();

private:
int maxSize;
std::vector<std::string> myArray;
};
You don't even need a destructor anymore.

Regards,

Paul McKenzie