473,511 Members | 15,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 1767
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5574
by: Avinash | last post by:
Hi, Why Overloaded operator cannot be a friend ? Thanking You. Avinash
9
2270
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
12
3273
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
10
2285
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
3
2932
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
13
5001
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
7
2777
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
9
1479
by: waltbrad | last post by:
For an exercise I had to write a class that would do math operations on complex numbers. The main point of the exercise was overloading various operators. One of them was the conjugate operator...
8
2951
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.