Hi all,
Below is a short program where I define a class A, in which I redefine
the operator =.
Inside the operator implementation, it just prints a line to notify
that it is indeed the new assignment operator which is called.
In main(), I define two variables a and b, both of the same type
A<int>. Then I write "a = b", where I expect that the new operator=
will be called. I expect that the compiler can figure out that "T =
int" and "S = int".
However, this is not the case. As the program is running, the new
operator is not called. It is the default operator= which is used.
Now, if I change the definition of "b" to "A<float> b", then the new
operator= is used.
Why the compiler cannot figure out in the first case that "T = int"
and "S = int" ?
------------------------
#include <iostream>
using namespace std;
template <class T>
class A
{
public:
template <class S>
A<T>& operator = (const A<S>& a)
{
cerr << "inside operator =" << endl;
return *this;
}
};
int main()
{
A<int> a;
A<int> b;
a = b; // the redefined assingment operator is NOT called.
// If I declare A<float> b, then the new operator is
called.
}
-------------------------------
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|