I have two versions of operator():
const A& operator(int x ,int y) const;
A& operator(int x ,int y);
But I don't know what would be the best way to avoid code repetition (because they essentially have the same logic).
I did read the awesome FAQ regarding operator overloading (and some other questions) but did not find answer.
EDIT_1: I've tried the suggestion that Skizz proposed but something like that does not quite work for me but maybe I've missed something:
Because this works while this does not compile giving the following error:
error: invalid initialization of reference of type ‘int&’ from expression of type ‘const value_type {aka const int}’
If both methods 'essentially have the same logic' then surely they should both be marked const. If they do behave differently, should they be called the same thing?
What you need is a third, hidden method that does the common thing, like this:-
const A& operator(int x ,int y) const
{
return Common (x,y);
}
A& operator(int x ,int y)
{
return Common (x,y);
}
A& Common (int x, int y) const
{
// common code
}
This takes advantage of the implicit non-const to const conversion. In the first method, the this object remains const in the call to Common but the non-const return value is converted to const. In the second, the non-const this object is converted to a const this object and the return object is unchanged.
If you have the data in question as shared/unique pointer, you are able to forward it to a common function:
class X {
public:
const A& operator () (int x ,int y) const {
common(data, x, y);
return *data;
}
A& operator () (int x ,int y) {
common(data, x, y);
return *data;
}
private:
void common(std::shared_ptr<T>, int x ,int y) const;
std::shared_ptr<A> data;
};
Now you are able to access the T* via data.get() which is 'T* get() const'.
(Note: I consider the common function a bad design)
You might do, also:
class X {
public:
const A& operator () (int x ,int y) const {
common(x, y);
return data;
}
A& operator () (int x ,int y) {
// ... make mutations
common(x, y);
// ... make mutations
return data;
}
void common(std::shared_ptr<T>, int x ,int y) const;
T data;
};
Or actually, making the data mutable, if it is not breaking logical const:
class X {
const A& operator () (int x ,int y) const {
// ... make mutations on the mutable data, only
return *lazy_evaluated_data;
}
A& operator () (int x ,int y) {
const X& self = *this;
self(x, y);
return *lazy_evaluated_data;;
}
private:
mutable T lazy_evaluated_data; // or a cache
};
Write a helper function and call it from both operators. You'll need to do some const_cast<...>(this) which can be ugly, but it's a common pattern.
Related
According to boost documentation - proper usage of boost::operators is to derive from it:
class A : boost::operators<A>
{
public:
bool operator < (const A&) const { return false; }
};
Now, I can use > and <= and >= because all of these operators can be implemented with <, see code snippet from boost:
template <class T, class B = operators_detail::empty_base<T> >
struct less_than_comparable1 : B
{
friend bool operator>(const T& x, const T& y) { return y < x; }
friend bool operator<=(const T& x, const T& y) { return !static_cast<bool>(y < x); }
friend bool operator>=(const T& x, const T& y) { return !static_cast<bool>(x < y); }
};
And finally less_than_comparable1 is one of boost::operators base class.
PROBLEM:
But adding such inheritance is not always convenient. E.g. this inheritance means I have to add constructor(s) to some structs, otherwise all old code, such as A{1} stops compiling:
struct A : boost::operators<A>
{
A() = default;
A(int a, int b = 0) : a(a), b(b) {}
int a;
int b;
};
bool operator < (const A&, const A&);
I tried several ways: inner class, static members of boost::operators<A> but it seems that only inheritance works.
I accept an answer that shows the way how to use boost::operators without inheritance.
I can also accept an answer, that explains why this inheritance is needed.
Ok, let's simplify a little this example, why I need inheritance in this very example below to get operator > from operator <?
template <typename A>
struct GtOperator
{
friend bool operator > (const A& l, const A& r)
{
return r < l;
}
};
struct A : private GtOperator<A>
{
bool operator < (const A&) const
{
return false;
}
};
int main() {
if (A{} > A{})
{
return -1;
}
}
Nothing else seems to work, e.g. this way does not work:
struct A
{
GtOperator<A> dummy;
bool operator < (const A&) const
{
return false;
}
};
Is it possible not to inherit from boost::operators, but still use it?
No, basically. It's intended to be inherited from. The reason it works is because argument-dependent lookup will only look for friend functions and function templates in associated classes ([basic.lookup.argdep]/4) - which are going to be A and A's base classes. If boost::operators<A> isn't a base class of A, its friend functions won't be found by name lookup.
Even with new aggregate initialization rules in C++17, A{1,2} would break because you'd have to write A{{},1,2}.
Your best bet is probably to write a macro that functions as a mixin that effectively accomplishes the same thing. So the ordering ones would be:
#define LESS_THAN_COMPARABLE(T) \
friend bool operator>(const T& x, const T& y) { return y < x; } \
friend bool operator<=(const T& x, const T& y) { return !static_cast<bool>(y < x); } \
friend bool operator>=(const T& x, const T& y) { return !static_cast<bool>(x < y); }
class A
{
public:
bool operator < (const A&) const { return false; }
LESS_THAN_COMPARABLE(A)
};
Yes, that kind of sucks. (Also these could be defined as non-member functions as well, just drop the friend and put the macro invocation outside of the class).
The other alternative, besides adding constructors and writing macros, is to hope that <=> comes to fruition and then wait a few years to be able to use it.
I am trying out with operator overloading, for which I wrote below code
class OwnClass
{
private:
int x,y;
public:
OwnClass(int x, int y) { SetX(x); SetY(y); }
int GetX() { return x; }
void SetX(int x) { this->x = x;}
int GetY() { return y; }
void SetY(int y) {this->y = y;}
OwnClass& operator + (const OwnClass &o) // Problematic line
{
this->x += o.GetX();
this->y += o.GetY();
return *this;
}
};
When compiled, following error is shown
fun.cpp(65): error C2662: 'OwnClass::GetX' : cannot convert 'this'
pointer from 'const OwnClass' to 'OwnClass &'
Conversion loses qualifiers
fun.cpp(66): error C2662: 'OwnClass::GetY' : cannot convert
'this' pointer from 'const OwnClass' to 'OwnClass &' Conversion loses
qualifiers
When I modify the code as under, it compiles fine.
OwnClass& operator + (OwnClass &o) // removed const
{
this->x += o.GetX();
this->y += o.GetY();
return *this;
}
I could not understand why so ? I mean I am not able to understand the compiler error.
The parameter o is declared as reference to const, which can't be called with GetX and GetY, because they're non-const member function. You can (and should) change them to const member functions to solve the issue.
int GetX() const { return x; }
int GetY() const { return y; }
BTW: In general binary operator+ is not supposed to return a reference to non-const. It's better to return a new object by value.
OwnClass operator + (const OwnClass &o) const
{
OwnClass r(GetX(), GetY());
r.x += o.GetX();
r.y += o.GetY();
return r;
}
Note for this case operator+ could (and should) be declared as const member function too. And as #M.M suggested, making it non-member function would be better.
The problem is that you are calling non-const member functions on const objects. Make getters const to fix this problem:
int GetX() const { return x; }
int GetY() const { return y; }
Suppose I have the following class:
class Point{
private:
int x,y;
public:
int get_x() const {return x;}
int get_y() const {return y;}
Point() :x(0),y(0){}
Point(int x,int y):x(x),y(y){}
Point(const Point& P){
x = P.get_x();
y = P.get_y();
}
Point& operator= (const Point& P) {
x = P.get_x();
y = P.get_y();
return *this;
}
friend ostream& operator<<(ostream& os,const Point& P) {
os<<"["<<P.get_x()<<", "<<P.get_y()<<"]";
return os;
}
Point operator - (const Point &P){
return Point(x-P.get_x(),y-P.get_y());
}
friend bool operator > (const Point &A, const Point &B) {
return A.get_y()>B.get_y();
}
};
Here I used friend function. I can also use function without friend:
class Point{
...
bool operator > (const Point &B) const {
return y>B.get_y();
}
...
};
What are the differences between them in actual implementations? Also in the second method, the code won't compile without 'cont', why is that? Even after I changed the getter function into non-const function, it still won't compile without the 'const'.
As you've already noticed, comparison operator overloads can either be implemented as a member function or as a non-member function.
As a rule of thumb you should implement them as a non-member non-friend function where possible, as this increases encapsulation, and it allows (non-explicit) conversion constructors to be used on either side of the operator.
Say for instance your Point class for whatever reason had an int conversion constructor:
Point(int x);
With a non-member comparison operator you can now do the following:
Point p;
p < 3; // this will work with both a member and non-member comparison
3 < p; // this will **only** work if the comparison is a non-member function
You also seem to be confused about when to use const, again as a rule of thumb for comparison operators you should always use const wherever possible, because comparisons logically do not involve any change to the object.
As Point is a very small class you could also take it by value instead, so in order of most to least preferable your options are:
// Non-member, non-friend
bool operator>(Point const& A, Point const& B);
bool operator>(Point A, Point B);
// Non-member, friend
friend bool operator>(Point const& A, Point const& B);
friend bool operator>(Point A, Point B);
// Member
bool Point::operator>(Point const& B) const;
bool Point::operator>(Point B) const;
How can I define operator ** such that it can perform exponentiation of 2 numbers . eg 2 ** 3. It should give answer as 8.
Or Indirectly is there any way I can do this with operator overloading instead of #define macros ?
You can't. You can only overload existing operators, and not for built in types.
You can't. You can only overload existing operators in C++; you cannot add new ones, or change the arity or associativity of existing operators. Even the preprocessor is powerless here - its identifiers cannot be symbols.
If you're willing to make a compromise w.r.t. ** and feel like obfuscating your code:
#include <cmath>
#include <iostream>
struct foo {
foo(int i) : i_(i) {}
int operator*(int exp)
{
return std::pow(i_,exp);
}
private:
int i_;
};
struct bar {
} power_of;
foo operator*(int i, bar)
{
return foo{i};
}
int main()
{
std::cout << 2 *power_of* 3; // prints 8
}
Otherwise, just use std::pow.
Like the other answers noted, this isn't possible for built-in types BUT you can get this to work for custom types like so (minimum code sample):
#include <cmath>
#include <iostream>
struct dummy;
struct Int
{
int i;
Int() : i(0) {}
Int(const int& i) : i(i) {}
dummy operator*();
};
struct dummy
{
Int* p;
dummy(Int* const p) : p(p) {}
int& operator*()
{
return p->i;
}
};
dummy Int::operator*()
{
return dummy(this);
}
int operator*(const Int& lhs, const dummy& rhs)
{
return std::pow(lhs.i, rhs.p->i);
}
int main()
{
Int a(2);
Int b(2);
std::cout<< a ** b << std::endl;
}
Live example
As others have noted: that isn't possible. You can overload another operator, like ^, for exponentiation, instead however on a simple type wrapper class/object.
But, if you're adventurous, another way, is to create a micro DSL that supports on-the-fly calculation of such an operator. (A famous example of that is LISP in C++)
However, given the effort involved, it may or may not be your cup of tea. However, it's worth knowing that such a possibility exists.
UPDATE:
Operator-Overloading works by overloading already existing operators. Why? Because if you could define your own, you will also have to define the precedence of such operators which can easily give way to abusing operators by abstracting away their original purpose - which increases difficulty when reading code. (At least that's the argument that's been made).
The closest operator that has a semantic meaning close to ** is the caret operator. A naive and illustrative implementation of such an operator is:
#include <iostream>
#include <cmath>
class Int {
public:
Int() {}
Int(int i) : value(i) {}
friend double operator^(const int& i, const Int& integer);
friend double operator^(const Int& integer, const int& i);
friend double operator^(const Int& lhs, const Int& rhs);
private:
int value;
};
double operator^ (const int& lhs, const Int& rhs) {
return std::pow(lhs, rhs.value);
}
double operator^ (const Int& lhs, const int& rhs) {
return std::pow(lhs.value, rhs);
}
double operator^ (const Int& lhs, const Int& rhs) {
return std::pow(lhs.value, rhs.value);
}
int main() {
Int i1 = 10;
Int i2 = 3;
double result = i1 ^ i2;
std::cout << result;
return 0;
}
Unfortunately the set of operators that can be overloaded in C++ is fixed and does not include the ** operator. You might think of using operator^() instead, but it turns out that ^ has the wrong precedence to serve as an exponentiation operator.
In short, there's not much you can do about this, unfortunately.
You can't overload operators for built-in types. I'd use operator ^ for such purpose for custom types.
Let's say I have a container class called MyContainerClass that holds integers.
The [] operator, as you know, can be overloaded so the user can more intuitively access values as if the container were a regular array. For example:
MyContainerClass MyInstance;
// ...
int ValueAtIndex = MyInstance[3]; // Gets the value at the index of 3.
The obvious return type for operator[] would be int, but then the user wouldn't be able to do something like this:
MyContainerClass MyInstance;
MyInstance[3] = 5;
So, what should the return type for operator[] be?
The obvious return type is int& :)
For increased elaboration:
int &operator[](ptrdiff_t i) { return myarray[i]; }
int const& operator[](ptrdiff_t i) const { return myarray[i]; }
// ^ could be "int" too. Doesn't matter for a simple type as "int".
This should be a reference:
int &
class MyContainerClass {
public:
int& operator[](unsigned int index);
int operator[](unsigned int index) const;
// ...
};
Returning a reference lets the user use the result as an lvalue, as in your example MyInstance[3] = 5;. Adding a const overload makes sure they can't do that if MyInstance is a const variable or reference.
But sometimes you want things to look like that but don't really have an int you can take a reference to. Or maybe you want to allow multiple types on the right-hand side of MyInstance[3] = expr;. In this case, you can use a dummy object which overloads assignment:
class MyContainerClass {
private:
class Index {
public:
Index& operator=(int val);
Index& operator=(const string& val);
private:
Index(MyContainerClass& cont, unsigned int ind);
MyContainerClass& m_cont;
unsigned int m_ind;
friend class MyContainerClass;
};
public:
Index operator[](unsigned int ind) { return Index(*this, ind); }
int operator[](unsigned int ind) const;
// ...
};
int&
returning a reference allows you too use the returned value as a left-hand side of the assignment.
same reason why operator<<() returns an ostream&, which allows you to write cout << a << b;