By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,959 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,959 IT Pros & Developers. It's quick & easy.

What is the difference between an operator and a member function inCRTP?

P: n/a
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&copy(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
}
Sep 16 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Peng Yu wrote:
#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;
You probably want to return something here.
}
template <typename D1>
B<D&copy(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
}
The following has no compilation errors:

#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;
return *this;
}
template <typename D1>
B<D&copy(const B<D1&that) {
_x = that._x;
return *this;
}
int _x;
};

struct D1 : public B<D1{
using B<D1>::operator=;

void print() {
std::cout << "D1" << std::endl;
}
};

struct D2 : public B<D2{
using B<D2>::operator=;

void print() {
std::cout << "D2" << std::endl;
}
};

int main() {
struct D1 d1;
d1.print();
struct D2 d2;
d2.print();

d1.copy(d2);
d1 = d2;
}

As you can see, a using directive is needed to make the operator visible.
Best

Kai-Uwe Bux
Sep 16 '08 #2

P: n/a
On Sep 16, 11:51 pm, Peng Yu <PengYu...@gmail.comwrote:
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&copy(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<Dconst& ) 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<Dconst& 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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 17 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.