Ah, makes sense. Almost easier to just make a new function.
One more quick question that I can't find the answer to:
I have a class, let's call it "Dual". Two of its members are two of these Matrices:
1 2 3 4 5 6 7
|
class Dual{
public:
Matrix3d firstOne;
Matrix3d secondOne;
};//Dual
| |
Now, let's say Matrix also has two constructors, one that takes no arguments and just makes it a 1x1x1 matrix, and one that takes three ints, and makes those the dimensions of the matrix.
Now, let's say I want Dual to have a constructor that can take three ints, and make those the dimensions of both of the arrays. I thought I could do:
1 2 3 4 5 6 7 8 9 10 11
|
class Dual{
public:
Matrix3d firstOne;
Matrix3d secondOne;
Dual(int a, int b, int c){
//Make the two matrices have dimensions a x b x c.
}
};//Dual
| |
But the matrices have already been given the dimensions of 1x1x1 from their default constructor! I can only think of a couple ways to go around this, but none of them seem like what's probably the right way.
One way I can see to do it would be to have two pointers to Matrix3d's, and then construct the actual objects from heap memory and call their constructors there:
1 2 3 4 5 6 7 8 9 10 11 12
|
class Dual{
public:
Matrix3d * firstOne;
Matrix3d * secondOne;
Dual(int a, int b, int c){
firstOne = new Matrix3d(a,b,c);
secondOne = new Matrix3d(a,b,c);
}
};//Dual
| |
Another way would be to make some sort of function like initMatrix(int a, int b, int c), where I would delete the useless array that the default constructor gave to the matrix and replace it with one of the dimensions I want:
1 2 3 4 5 6 7 8 9 10 11 12
|
class Dual{
public:
Matrix3d firstOne;
Matrix3d secondOne;
Dual(int a, int b, int c){
firstOne.initMatrix3d(a,b,c);
secondOne.initMatrix3d(a,b,c);
}
};//Dual
| |
that uses:
1 2 3 4 5 6 7 8 9 10 11
|
class Matrix3d{
public:
double * dataArray;
void initMatrix3d(int a, int b, int c){
delete [] dataArray;
dataArray = new double[a*b*c];
}
};//Matrix3d
| |
How should I go about this?