S
Silver
Hi everyone,
I want to write a class with an NxM array as a private member. I want the
memory for this array to be dynamically allocated. I'm a bit confused...
I would like to know how this can be done both with malloc/free and
new/delete.
(Btw, is there any case where malloc/free is better than new/delete?)
Here's what I though (code follows)
Thanks
class A
{
private:
float** ppa;
int fl; //flag
public:
A(int n, int m); // Constructor
A(); // Default constructor
~A(); // Destructor
};
A::A(int n, int m) {
cout << endl << "Constructor for class A called.";
fl = 1;
ppa = (float **)malloc(n * sizeof(float *));
for(int i = 0; i < n; i++)
ppa = (float *)malloc(m * sizeof(float));
}
A::A() {
cout << endl << "Default constructor for class A called.";
fl = 0;
}
A::~A() {
if(fl) {
free((void **)cpp);
cout << endl << "Destruvtor for class A called.";
}
}
I want to write a class with an NxM array as a private member. I want the
memory for this array to be dynamically allocated. I'm a bit confused...
I would like to know how this can be done both with malloc/free and
new/delete.
(Btw, is there any case where malloc/free is better than new/delete?)
Here's what I though (code follows)
Thanks
class A
{
private:
float** ppa;
int fl; //flag
public:
A(int n, int m); // Constructor
A(); // Default constructor
~A(); // Destructor
};
A::A(int n, int m) {
cout << endl << "Constructor for class A called.";
fl = 1;
ppa = (float **)malloc(n * sizeof(float *));
for(int i = 0; i < n; i++)
ppa = (float *)malloc(m * sizeof(float));
}
A::A() {
cout << endl << "Default constructor for class A called.";
fl = 0;
}
A::~A() {
if(fl) {
free((void **)cpp);
cout << endl << "Destruvtor for class A called.";
}
}