ticster
ticster

Reputation: 786

Modifying overloaded member operator using constness

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 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."

Upvotes: 2

Views: 182

Answers (3)

Shawnone
Shawnone

Reputation: 860

For non-static member function. There is an implied object argument - the this pointer. And if a non-static member function is declared const, the this pointer would be const. So actually, your two functions looks like:

TWDataPointer<T> operator () (this, const TWMatrix<int> & I1, const TWMatrix<int> & I2)
{
     return TWDataPointer<T>(*this,I1,I2);
}

TWMatrix<T> operator () (const this, const TWMatrix<int> & I1, const TWMatrix<int> & I2) const 
{
    return SubMat(I1,I2);
}

I think now it is clear that why when your D5 is not const, the second function would never be called. You can use

TWMatrix<T> matrix = ((const TWMatrix<T>&)D5)(I1,I2);

as a workaround. But I don't think it is a good idea. It is very tricky that const and non-const methods get different result types.

For your int example, If the test is not const, I don't think the first function will be called.

Upvotes: 0

Jason
Jason

Reputation: 32510

Is there a specific reason that you need to have only a single const of the second method? From what it appears to me in this code, and the possible use-scenarios, while your second method is technically a const method, it doesn't have to be declared as-such. In other word it's a method that can be used by both const and non-const versions of your class instance, and you're obviously coming up with scenarios where your class instance requires the method when it's not a const object. Thus the use of labeling your method as a const method just because it doesn't change the state of the instance that calls the method doesn't mean that you must label your method as const.

In order to work with both scenarios, I would generate two versions of operator() that return the TWMatrix<T> type ... one that is declared const, and one that is declared as a non-const method so that it can be utilized by both const and non-const class instances. The downside though is you would have to create a separate method for returning the TWDataPointer<T> type since you can't overload based on just the return-type.

Upvotes: 2

Nim
Nim

Reputation: 33655

At a guess, this is because D5 is not constant. It's the cv-qualification of this object which determines which operator is selected.

It seems like you are using the const qualification to disambiguate operator. I would use a different technique (for example a template parameter which is the output type you want, or the template parameter is the policy which defines how the return type is constructed, or even simpler, specify an output parameter which is what you want)

Upvotes: 1

Related Questions