Here’s an overview of my problem:
I have a templated class called TWMatrix. I’ve overloaded the () operator such that it can take in a pair of TWMatrix and extract the corresponding entries. I want the “extraction” to produce 2 distinct results for 2 distinct cases. This has worked before when taking a pair of ints, but I’m a very stuck getting it to work for this slightly different problem:
- In one case, I want the operator to copy the entries into another TWMatrix and output it.
- In another case, I want the operator to copy the addresses of these entries into an object of type TWDataPointer, output that object.
In the first case, the method does not break constness. Indeed, the entries themselves are neither modified nor vulnerable to future modifications. In the latter, the whole point is that the TWDataPointer object can be used to modify those entries of the matrix. And so the corresponding bit of code is:
TWDataPointer<T> operator () (const TWMatrix<int> & I1, const TWMatrix<int> & I2)
{
return TWDataPointer<T>(*this,I1,I2);
}
TWMatrix<T> operator () (const TWMatrix<int> & I1, const TWMatrix<int> & I2) const
{
return SubMat(I1,I2);
}
SubMat is a method that creates a matrix with the corresponding entries. It works just fine, and what values it fetches isn’t very relevant here. The issue is that this second operator seems to never get called. So for example if, in my main, I write:
TWMatrix<double> test = D5(I1,I2);
The compiler complains saying:
error: conversion from ‘TWDataPointer<double>’ to non-scalar type ‘TWMatrix<double>’ requested
This is very unlike:
T operator () (const int & i, const int & j) const
{
return data[j+i*nc];
}
T& operator () (const int & i, const int & j)
{
return data[j+i*nc];
}
Which works exactly as expected, and returns either a T or T& depending on the situation. From what I understand of overloading operators, it’s the const that lets the compiler distinguish which to use in which situation. So why won’t this work?
Thanks in advance for the help, and feel free to ask for any extra pieces of code you may need.
PS: I already have a workaround in mind for this, and though it’s not immensely ugly, it’s nowhere near as simple, elegant and robust as this would be if I could get it to work.
edit: Thanks for the help so far, and I’m still looking forward to hearing your answers. In particular, I’m still confused about this:
“Going back to the int example, why does a = test(0,0) use the const version of the corresponding operator even if test is not declared const? Indeed, I’ve checked that this is the case using cout statements in both versions of the () operators.”
Is there a specific reason that you need to have only a single
constof the second method? From what it appears to me in this code, and the possible use-scenarios, while your second method is technically aconstmethod, it doesn’t have to be declared as-such. In other word it’s a method that can be used by bothconstand non-constversions of your class instance, and you’re obviously coming up with scenarios where your class instance requires the method when it’s not aconstobject. Thus the use of labeling your method as aconstmethod just because it doesn’t change the state of the instance that calls the method doesn’t mean that you must label your method asconst.In order to work with both scenarios, I would generate two versions of
operator()that return theTWMatrix<T>type … one that is declaredconst, and one that is declared as a non-constmethod so that it can be utilized by bothconstand non-constclass instances. The downside though is you would have to create a separate method for returning theTWDataPointer<T>type since you can’t overload based on just the return-type.