P
Peng Yu
Hi,
In the following code, the 'copy' member function works. But the '='
operator does not work. Can somebody let me know why a member function
is different from an operator.
Thanks,
Peng
#include <iostream>
template <typename D>
class B {
public:
void print() {
static_cast<D*>(this)->print();
}
template <typename D1>
B<D> &operator=(const B<D1> &that) {
_x = that._x;
}
template <typename D1>
B<D> ©(const B<D1> &that) {
_x = that._x;
return *this;
}
int _x;
};
struct D1 : public B<D1> {
void print() {
std::cout << "D1" << std::endl;
}
};
struct D2 : public B<D2> {
void print() {
std::cout << "D2" << std::endl;
}
};
int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();
d1.copy(d2);
d1 = d2;//error
}
In the following code, the 'copy' member function works. But the '='
operator does not work. Can somebody let me know why a member function
is different from an operator.
Thanks,
Peng
#include <iostream>
template <typename D>
class B {
public:
void print() {
static_cast<D*>(this)->print();
}
template <typename D1>
B<D> &operator=(const B<D1> &that) {
_x = that._x;
}
template <typename D1>
B<D> ©(const B<D1> &that) {
_x = that._x;
return *this;
}
int _x;
};
struct D1 : public B<D1> {
void print() {
std::cout << "D1" << std::endl;
}
};
struct D2 : public B<D2> {
void print() {
std::cout << "D2" << std::endl;
}
};
int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();
d1.copy(d2);
d1 = d2;//error
}