Is it possible to call this operator with a pointer?

Hey guys. I have an operator defined for a matrix class, so I can do

int someInt = myMatObj(1,4,5);

Or something to get the value at that location. But let's say I only have a pointer to the Matrix object for some reason:

Matrix3d * matPtr = new Matrix3d;

Can I use that operator somehow? I'm getting errors when I try calling it the way I would call a normal member function with a pointer like this:

int someInt = matPtr->(1,4,5);

I know it would be relatively simple to just make another member function like getValuesAt(int a, int b, int c) (which is what I'm currently doing), but I'm curious. Can you do it just with the operator?

Thanks!
Last edited on
myPtr -> operator() (1,4,5)
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?
Last edited on
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) : firstOne(a, b c), secondOne(a, b, c) 
        {
        }

};//Dual  


--Rollie
Sorry, how does that work? Won't the default constructors for the two matrices be called anyway?
Nope, the above tells the compiler to call the constructor Matrix3d(int, int, int); when constructing firstOne and secondOne, instead of the default constructor.

--Rollie
Awesome! Thanks!
myPtr -> operator() (1,4,5)

how about (*myPtr)(1, 4, 5)?..
Topic archived. No new replies allowed.