Template, Inheritance and Operator Overloading

D

danilo.horta

Hi folks

I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from correct
one, the base class.

Code:
// VecBasis.h file
template<class T, size_t numDim> class VecBasis {
protected:
        valarray<T> arr;

public:
        VecBasis<T, numDim>() : arr(numDim) {}
        VecBasis<T, numDim>(const VecBasis<T, numDim>& v) : arr(numDim)
{/**/}
// ...
        VecBasis<T, numDim> operator * (T v) const {/**/}
};

// Vec3.h file
template<class T> class Vec3 : public VecBasis<T, 3> {
public:
	Vec3<T> () : VecBasis<T, 3>() {}

	T x() const { return VecBasis<T, 3>::arr[0]; } // is the scope
operator really necessary here?
// ...
        T operator * (const Vec3<T>& v) {/**/} // here is the problem
};

// main.cpp file
int main()
{
        Vec3<float> v0;
        v0.operator*(2.0f); // error
}

When I try to compile the code, it says,
.../../src/main.cpp:17: error: no matching function for call to
'Vec3<float>::eek:perator*(float)'
.../../src/math/Vec3.h:21: note: candidates are: T
Vec3<T>::eek:perator*(const Vec3<T>&) [with T = float]

If I comment out the Vec3::eek:perator*(), the compiler can guess the
right operator function (from base class).

Perhaps I could use the explicit keyword (just guessing) to prevent
implicit casting and the compiler would simply ignore the operator from
derived class and use the correct one, but this would give me much more
trouble. Any idea?

One more thing: is that scope resolution operator obligatory? the
compiler complaints if I don't use it.

Thanks :)
 
V

Victor Bazarov

I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from
correct one, the base class.

The derived one _hides_ the base one. Period. If you want them in
the scope of the derived class _together_, use 'using'.

V
 
D

danilo.horta

Victor said:
I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from
correct one, the base class.

The derived one _hides_ the base one. Period. If you want them in
the scope of the derived class _together_, use 'using'.

V

"Overload resolution is not applied across different class scopes" -
Stroustrup. I understand now. Thanks.
 
F

Ferdi Smit

// Vec3.h file
template<class T> class Vec3 : public VecBasis<T, 3> {
public:
Vec3<T> () : VecBasis<T, 3>() {}

T x() const { return VecBasis<T, 3>::arr[0]; } // is the scope
operator really necessary here?
// ...
T operator * (const Vec3<T>& v) {/**/} // here is the problem
}; uble. Any idea?

One more thing: is that scope resolution operator obligatory? the
compiler complaints if I don't use it.

Yes. You have a dependent base class (you derive from a class dependent
on the template parameter T), and therefore non-qualified names are not
looked up in the base class. There is good reason for this with respect
to template specialization. Suppose it would look up the member, decide
on it's type, and shortly after you specialize the base class for this
specific template T... the type of the base member now changes and the
original assumption is wrong. (I hope i said that right, if you need the
exact wording please browse the standard on dependent base classes...)

You can either qualify the name, as you've done, or you can use the
"this->arr[0]" notation. This (some pun huh?) achieves the same thing.

--
Regards,

Ferdi Smit (M.Sc.)
Email: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top