Re: What is the difference between an operator and a member function
in CRTP?
On Sep 16, 11:51 pm, Peng Yu <PengYu...@gmail.com> wrote:
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.
It's different from the assignment operator because the compiler
will implicitly declare and define a copy assignment operator if
you don't; the compiler will never implicitly declare and define
a named function.
#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;
};
For starters: a function template is never a copy assignment
operator. In the above, you also have a non-template B<D>&
B<D>::operator=( B<D> const& ) declared (and if used, defined)
by the compiler. In this case, it doesn't matter, because the
compiler defined function does exactly the same thing as your
function template, but you should be aware of it. The presence
of the operator as a function template, even a function template
which could be instantiated with the type itself, does NOT
inhibit implicit generation of the operator by the compiler. If
the operator= should do something other than what the default
operator= does, then you must also define a non-template
operator= taking a B<D> const& as argument.
struct D1 : public B<D1> {
void print() {
std::cout << "D1" << std::endl;
}
};
Note that the compiler has also provided an operator= function
for this class, which hide the operator= function of the base
class. It's exactly as if you'd declared a:
D1& operator=( D1 const& ) ;
You can use a using declaration to "unhide" the functions in the
base class, but this will still not prevent the compiler from
generating its version as well, which will be used if you assign
a D1 to another D1. (Templates and functions brought into the
class by means of a using declaration never inhibit the
automatic generation.)
Note that as a general rule, you will want to duplicate all of
the assignment operators in the derived class, since logically,
the assignment operators in the derived class should have a
different return type than those of the base class. (Note too
that when C++ derivation is used to implement OO inheritance,
you generally don't want to support assignment. But I'm far
from sure that that is the case here.)
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
Yep, because the only assignment operator in d1 is the compiler
generated one, which takes a d1.
}
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34