Let's say I have defined a class with an internal + operator and also an external + operator;
class MyClass {
public:
MyClass operator +();
};
MyClass operator +(const MyClass& a);
If in my main program I call
MyClass a;
MyClass b = +a;
What is being called, this (internal):
a.operator +()
or this (external)?:
operator +(a)
The same question for binary operators.
The member function is chosen: it can bind directly to the expression a, while the non-member function needs to convert MyClass to const MyClass before binding to the reference parameter. So calling the member involves a better conversion sequence, making that the best overload.
If you removed const from the non-member, or added const to the member, then both would be equally viable; you should get an error saying that the overload is ambiguous.
By fixing some ambiguities in the code, and doing some printing, the following code will give the answer Internal operator.
class MyClass {
public:
MyClass operator+() {
std::cout << "Internal operator." << std::endl;
return *this;
};
};
MyClass operator+(const MyClass& a) {
std::cout << "External operator" << std::endl;
return a;
}
int main() {
MyClass a, b;
b = +a;
return 0;
}
The internal operator is used because you cannot overload an operator with the same arguments once it already exists: you cannot define a + operator for strings that does something crazy because there already is one that concatenates them. That's the same case. You have a + operator defined inside the class, so the identical one( as function prototype ) becomes useless.
I did a little test myself:
#include <iostream>
#include "MyClass.h"
using namespace std;
MyClass operator +(const MyClass& a, const MyClass& b)
{ cout << "external" << endl; return a; }
int main() {
MyClass foo, bar;
foo + bar;
return 0;
}
class MyClass {
public:
MyClass operator+(const MyClass& a) { cout << "internal" << endl; return a; }
};
The output of the program was "internal".
My conclusion is that if there is an internal operator that fits the operation it will be the one taken. I can't see a way to make the external one to be taken if the internal fits better. But I must point out that I only did a little test and haven't learned somewhere that this is the 100% answer.
Related
I have a problem with creating an operator<< in a class in this particular situation. I have a class that wraps a std::ostream so I can do some preprocessing for some types or some conditions before passing to the ostream, and want to pass some things straight through. I don't want to inherit the std::ostream, unless there is a good argument that I should. (I think I tried it once and found great difficulty and no success.)
I cannot use a template function because the processing depends on type in some cases, and I think the ambiguity would remain between it and those for my specific types (like 'Stuff'). Do I have to resort to using typeid ??
class MyClass
{
private:
std::ostream & m_out;
public:
MyClass (std::ostream & out)
: m_out(out)
{}
MyClass & operator<< (const Stuff & stuff)
{
//...
// something derived from processing stuff, unknown to stuff
m_out << something;
return *this;
}
// if I explicitly create operator<< for char, int, and double,
// such as shown for char and int below, I get a compile error:
// ambiguous overload for 'operator<<' on later attempt to use them.
MyClass & operator<< (char c)
{
m_out << c; // needs to be as a char
return *this;
}
MyClass & operator<< (int i)
{
if (/* some condition */)
i *= 3;
m_out << i; // needs to be as an integer
return *this;
}
// ...and other overloads that do not create an ambiguity issue...
// MyClass & operator<< (const std::string & str)
// MyClass & operator<< (const char * str)
};
void doSomething ()
{
MyClass proc(std::cout);
Stuff s1, s2;
unsigned i = 1;
proc << s1 << "using stuff and strings is fine" << s2;
proc << i; // compile error here: ambiguous overload for 'operator<<' in 'proc << i'
}
Your problem is that the value you're trying to insert is unsigned while the overloads you've provided only work on signed types. As far as the compiler is concerned, converting unsigned to int or char are both equally good/bad and result in ambiguity.
I cannot use a template function because the processing depends on type in some cases
Just make overloadings for those types.
I think the ambiguity would remain between it and those for my specific types (like 'Stuff').
No. If operator<< is overloaded for specific type, this overloading will be called. Otherwise will be called template function.
template <class T>
MyClass& operator<< (const T& t)
{
m_out << t;
return *this;
}
I'm trying to understand the concept of operator overloading by writing some simple, silly tests. I thought this might be useful as this helps me understand C++ better.
Why does this example implementing a concatenation operator of Animal class and std::string not compile? G++ gives me the following error:
extra qualification 'Animal::' on member 'operator+' [-fpermissive]
This is the code:
#include <iostream>
using namespace std;
class Animal {
public:
string _type;
string _name;
string _sound;
Animal & Animal::operator+(const string & o);
};
Animal & Animal::operator+(const string & o) {
cout << "plus operator \n";
this->_name=o;
return *this;
}
int main( int argc, char ** argv ) {
Animal a;
a+"hhh";
cout<<a._name;
return 0;
}
Animal & Animal::operator+(const string & o);
Is invalid. It should be:
Animal & operator+(const string & o);
Also, your implementation of a simple addition operator, results in the modifications of one of the operands. This is never a good thing for an addition operator.
For example:
int a, b = 5, c = 3;
a = b + c;
That doesn't change the values of either operands; it leaves b and c untouched, and returns an entirely different instance.
You should therefore not overload the addition operator, but the addition assignment compound operator (+=):
Animal & operator+=(const string & o);
And of course change the implementation and calls to it accordingly:
Animal & Animal::operator+=(const string & o) {
cout << "plus operator \n";
this->_name=o;
return *this;
}
And:
a += "hhh";
The declaration of operator+ inside your class does not need to be qualified, precisely because it is being declared within the class:
class Animal {
// ...
Animal& operator+(const string& o);
}
This qualification is necessary when you define the function because you define it outside the class - the compiler needs to know to which class the function belongs.
There's no need for the Animal:: in the prototype, because it's inside the Animal class already. Just use:
Animal & operator+(const string & o);
The Animal:: qualification should be used in the definition of a member function, not in the declaration. Thus, change your operator declaration into:
Animal & operator+(const string & o);
Anyone got an idea on how to write an operator for a class that isn't a member function of the class?
Just make it a free function, or a friend function. A good example of this is operator<<:
class X {
public:
int x;
}
ostream& operator<< (ostream& os, const X& x) {
os << x.x;
return os;
}
The benefit of making it a friend function is that you have direct access to private members, whereas a free function must access all members via public methods.
Arithmetic operators, stream operators, et cetera are often not members of a class. However, they may need to be friends in order to access private data members.
I prefer not to use friend and to expose methods that can be used by the operators instead. I believe this to be more in keeping with the Open/closed principle, as I could easily add a subtraction operator without editing the class.
These are handy for unit-testing, too (I can "inject" a std::ostringstream to test the output of print(), for instance).
Here is an example:
#include <iostream>
class Number
{
public:
Number(int j)
:i(j)
{
}
void print(std::ostream& os) const
{
os << i;
}
int value() const
{
return i;
}
private:
int i;
};
std::ostream& operator <<(std::ostream& os, const Number& n)
{
n.print(os);
return os;
}
Number operator +(const Number& n, const Number& o)
{
return Number(n.value() + o.value());
}
int main()
{
Number a(4), b(5), c(a + b);
std::cerr << c << std::endl;
}
Just declare the global function with the operator name:
Point operator+(Point& p, Vector& v) {
return new Point(p.x + q.i, p.y + q.j);
}
Basically, you can take the operator out of the class, and add a parameter to the beginning of the parameter list. In many cases, you will also need to declare the operator function as a friend.
For instance
class Foo
{
Foo operator +( Foo const& other );
};
becomes
class Foo
{
friend Foo operator +( Foo const&, Foo const& );
};
Foo operator +( Foo const& first, Foo const& second );
The friend statement allows the operator to still access any private or protected members needed.
Note that there are some restrictions on which operators can be overloaded in this manner. See this article for such a list.
I know I can answer this question easily for myself by generatin the code and see if it compiles. But since I couldn't find a similar question, I thought it's knowledge worth sharing.
Say I am overloading the + operator for MyClass. Can I overload it multiple times. Different overload for different types. Like this:
class MyClass{
...
inline const MyClass operator+(const MyClass &addend) const {
cout<<"Adding MyClass+MyClass"<<endl;
...//Code for adding MyClass with MyClass
}
inline const MyClass operator+(const int &addend) const {
cout<<"Adding MyClass+int"<<endl;
...//Code for adding MyClass with int
}
...
};
int main(){
MyClass c1;
MyClass c2;
MyClass c3 = c1 + c2;
MyClass c4 = c1 + 5;
}
/*Output should be:
Adding MyClass+MyClass
Adding MyClass+in*/
The reason I want to do this is that I am building a class that I want to be as optimized as possible. Performance is the biggest concern for me here. So casting and using switch case inside the operator + overloaded function is not an option. I f you'll notice, I made both the overloads inline. Let's assume for a second that the compiler indeed inlines my overloads, then it is predetermined at compile time which code will run, and I save the call to a function (by inlining) + a complicated switch case scenario (in reality, there will be 5+ overloads for + operator), but am still able to write easily read code using basic arithmetic operators.
So, will I get the desired behavior?
Yes.
These operator functions are just ordinary functions with the special names operator#. There's no restriction that they cannot be overloaded. In fact, the << operator used by iostream is an operator with multiple overloads.
The canonical form of implementing operator+() is a free function based on operator+=(), which your users will expect when you have +. += changes its left-hand argument and should thus be a member. The + treats its arguments symmetrically, and should thus be a free function.
Something like this should do:
//Beware, brain-compiled code ahead!
class MyClass {
public:
MyClass& operator+=(const MyClass &rhs) const
{
// code for adding MyClass to MyClass
return *this;
}
MyClass& operator+=(int rhs) const
{
// code for adding int to MyClass
return *this;
}
};
inline MyClass operator+(MyClass lhs, const MyClass& rhs) {
lhs += rhs;
return lhs;
}
inline MyClass operator+(MyClass lhs, int rhs) {
lhs += rhs;
return lhs;
}
// maybe you need this one, too
inline MyClass operator+(int lhs, const MyClass& rhs) {
return rhs + lhs; // addition should be commutative
}
(Note that member functions defined with their class' definition are implicitly inline. Also note, that within MyClass, the prefix MyClass:: is either not needed or even wrong.)
Yes, you can overload operators like this. But I'm not sure what "switch case" you are referring to. You can live with one overload if you have a converting constructor
class MyClass{
...
// code for creating a MyClass out of an int
MyClass(int n) { ... }
...
inline const MyClass MyClass::operator+(const MyClass &addend) const {
cout<<"Adding MyClass+MyClass"<<endl;
...//Code for adding MyClass with MyClass
}
...
};
No switch is needed at all. This is eligible if "MyClass" logically represents a number.
Notice that you should overload these operators by non-member functions. In your code 5 + c1 would not work, because there is no operator that takes an int as left hand side. The following would work
inline const MyClass operator+(const MyClass &lhs, const MyClass &rhs) {
// ...
}
Now if you keep the converting constructor you can add the int by either side with minimal code overhead.
In a project I'm working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed.
Here's what I tried to do:
ostream & Score::operator<< (ostream & os, Score right)
{
os << right.getPoints() << " " << right.scoreGetName();
return os;
}
Here are the errors returned:
score.h(30) : error C2804: binary 'operator <<' has too many parameters
(This error appears 4 times, actually)
I managed to get it working by declaring the overload as a friend function:
friend ostream & operator<< (ostream & os, Score right);
And removing the Score:: from the function declaration in score.cpp (effectively not declaring it as a member).
Why does this work, yet the former piece of code doesn't?
Thanks for your time!
EDIT
I deleted all mentions to the overload on the header file... yet I get the following (and only) error. binary '<<' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion)
How come my test, in main(), can't find the appropriate overload? (it's not the includes, I checked)
Below is the full score.h
#ifndef SCORE_H_
#define SCORE_H_
#include <string>
#include <iostream>
#include <iostream>
using std::string;
using std::ostream;
class Score
{
public:
Score(string name);
Score();
virtual ~Score();
void addPoints(int n);
string scoreGetName() const;
int getPoints() const;
void scoreSetName(string name);
bool operator>(const Score right) const;
private:
string _name;
int _points;
};
#endif
Note: You might want to look at the operator overloading FAQ.
Binary operators can either be members of their left-hand argument's class or free functions. (Some operators, like assignment, must be members.) Since the stream operators' left-hand argument is a stream, stream operators either have to be members of the stream class or free functions. The canonical way to implement operator<< for any type is this:
std::ostream& operator<<(std::ostream& os, const T& obj)
{
// stream obj's data into os
return os;
}
Note that it is not a member function. Also note that it takes the object to stream per const reference. That's because you don't want to copy the object in order to stream it and you don't want the streaming to alter it either.
Sometimes you want to stream objects whose internals are not accessible through their class' public interface, so the operator can't get at them. Then you have two choices: Either put a public member into the class which does the streaming
class T {
public:
void stream_to(std::ostream&) const {os << obj.data_;}
private:
int data_;
};
and call that from the operator:
inline std::ostream& operator<<(std::ostream& os, const T& obj)
{
obj.stream_to(os);
return os;
}
or make the operator a friend
class T {
public:
friend std::ostream& operator<<(std::ostream&, const T&);
private:
int data_;
};
so that it can access the class' private parts:
inline std::ostream& operator<<(std::ostream& os, const T& obj)
{
os << obj.data_;
return os;
}
Let's say you wanted to write an operator overload for + so you could add two Score objects to each other, and another so you could add an int to a Score, and a third so you could add a Score to an int. The ones where a Score is the first parameter can be member functions of Score. But the one where an int is the first parameter can't become member functions of int, right? To help you with that, you're allowed to write them as free functions. That is what is happening with this << operator, you can't add a member function to ostream so you write a free function. That's what it means when you take away the Score:: part.
Now why does it have to be a friend? It doesn't. You're only calling public methods (getPoints and scoreGetName). You see lots of friend operators because they like to talk directly to the private variables. It's ok by me to do that, because they are written and maintained by the person maintaing the class. Just don't get the friend part muddled up with the member-function-vs-free-function part.
You're getting compilation errors when operator<< is a member function in the example because you're creating an operator<< that takes a Score as the first parameter (the object the method's being called on), and then giving it an extra parameter at the end.
When you're calling a binary operator that's declared as a member function, the left side of the expression is the object the method's being called on. e.g. a + b might works like this:
A a;
B b
a.operator+(b)
It's typically preferable to use non-member binary operators (and in some cases -- e.g. operator<<for ostream is the only way to do it. In that case, a + b might work like this:
A a;
B b
operator+(a, b);
Here's a full example showing both ways of doing it; main() will output '55' three times:
#include <iostream>
struct B
{
B(int b) : value(b) {}
int value;
};
struct A
{
A(int a) : value(a) {}
int value;
int operator+(const B& b)
{
return this->value + b.value;
}
};
int operator+(const A& a, const B& b)
{
return a.value + b.value;
}
int main(int argc, char** argv)
{
A a(22);
B b(33);
std::cout << a + b << std::endl;
std::cout << operator+(a, b) << std::endl;
std::cout << a.operator+(b) << std::endl;
return 0;
}