I would like to add an option to make a chained comparison on objects of MyClass. For example:
MyClass one;
MyClass two;
MyClass three;
/*....*/
if (one == two == three){
/*....*/
}
So that if all are equal it will return true, else it will return false.
I currently have this operator overload that works fine when comparing only two instances of MyClass:
bool operator==(const MyClass &other);
I understand that one==two==three is equal to ((one==two)==three) so I guess that should change my operator== to be something like:
MyClass& operator==(const MyClass &other);
But I couldn't manage to understand how to complete this in order to be able to compare more than two instances in a row (chained).
As pointed out in comments, it's not good to break the usual semantics in such a way. The Principle of least Astonishment should be followed as mentioned.
But I couldn't manage to understand how to complete this in order to be able to compare more than two instances in a row (chained).
Not that I think it's really a good idea to do so1, but here's a working solution:
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int x_) : x(x_), isfalse(false) {}
const MyClass& operator==(const MyClass& rhs) const {
if(!isfalse && x == rhs.x) {
return rhs;
}
return FalseInst;
}
operator bool() const {
return !isfalse;
}
private:
int x;
MyClass() : x(), isfalse(true) {}
const bool isfalse;
static MyClass FalseInst;
};
MyClass MyClass::FalseInst;
int main()
{
MyClass one(1);
MyClass two(1);
MyClass three(1);
if(one == two == three) {
cout << "Yay!" << endl;
}
MyClass four(1);
MyClass five(0);
MyClass six(0);
if(!(four == (five == six))) {
cout << "Yay!" << endl;
}
}
Live Demo
1) Note the warning issued by the compiler.
As a purely theoretical problem, here is a way to solve it:
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int x = 0) : x(x) {}
struct Cmp {
const A *ptr;
mutable bool val;
operator bool() const {
return val;
}
const Cmp &operator == (const A &other) const {
return other == *this;
}
};
bool isEqualTo (const A &other) const {
return x == other.x;
}
Cmp operator == (const A &other) const {
return {this, isEqualTo(other)};
}
const Cmp &operator == (const Cmp &other) const {
//other.val = other.val && (*this == *other.ptr).val;
other.val &= other.ptr->isEqualTo(*this);
return other;
}
};
int main() {
cout << (A(10) == A(10) == A(10)) << endl;
cout << (A(10) == A(9) == A(10)) << endl;
return 0;
}
Related
In the code, why is (10 != i) calling == instead of !=? The other two call !=
#include <iostream>
class Integer
{
int x;
public:
bool
operator== (const Integer &i)
{
std::cout << "==";
return x == i.x;
}
bool
operator!= (const Integer &i)
{
std::cout << "!=";
return x != i.x;
}
Integer (int t = 0) { x = t; }
};
int
main ()
{
Integer i;
std::cout << (i != i) << '\n'; // calls !=
std::cout << (i != 100) << '\n'; // calls !=
std::cout << (10 != i) << '\n'; // calls ==
}
Prior to C++20, you'd need to add two free functions for the comparison where the int is on the left-hand side:
bool operator==(int lhs, const Integer& rhs) {
return rhs == lhs;
}
bool operator!=(int lhs, const Integer& rhs) {
return rhs != lhs;
}
You should also make the member comparison operators const qualified:
class Integer {
public:
//...
bool operator==(const Integer &i) const { // note const
std::cout << "==";
return x == i.x;
}
bool operator!=(const Integer &i) const { // note const
std::cout << "!=";
return x != i.x;
}
//...
};
You could also remove the member operators to simplify things. Now the left-hand side int will be implicitly converted to Integer and then compared with the right-hand side:
class Integer {
int x;
public:
Integer(int t = 0) : x{t} {}
friend bool operator==(const Integer& lhs, const Integer& rhs) {
return rhs.x == lhs.x;
}
friend bool operator!=(const Integer& lhs, const Integer& rhs) {
return !(rhs == lhs);
}
};
Since you've tagged this C++20, you can let operator== do all the work. See Default comparisons. It'll be used for operator!= too.
class Integer {
int x;
public:
bool operator==(const Integer &i) const {
std::cout << "==";
return x == i.x;
}
Integer(int t = 0) : x{t} {}
};
... and it'll correctly show that it used operator== for all your != comparisons (and negated it).
More from Defaulted equality comparison:
A class can define operator== as defaulted, with a return value of bool. This will generate an equality comparison of each base class and member subobject, in their declaration order. Two objects are equal if the values of their base classes and members are equal. The test will short-circuit if an inequality is found in members or base classes earlier in declaration order.
Per the rules for operator==, this will also allow inequality testing
This means that you will in fact get away with the below only since C++20:
class Integer {
int x;
public:
bool operator==(const Integer &i) const = default;
Integer(int t = 0) : x{t} {}
};
or even better, get all the comparison operators for free by defaulting the spaceship operator <=>:
class Integer {
int x;
public:
auto operator<=>(const Integer &i) const = default;
Integer(int t = 0) : x{t} {}
};
There are two new additions to C++20 that made this possible (note that your code doesn't compile in earlier standard versions).
Compiler will attempt to replace a != b with !(a == b) if there is no suitable != for these arguments.
If there is no suitable a == b, compiler will attempt b == a as well.
So, what happens - compiler first notices that it doesn't know how to compare 10 != i, so it tries !(10 == i). There is still no suitable comparison, so it tries !(i == 10) and it can finally be done using your implicit constructor to convert 10 to Integer.
It can be easily verified by adding more info to debug print:
bool
operator== (const Integer &i) const
{
std::cout << x << "==" << i.x << ' ';
return x == i.x;
}
will print 0==10 1 in the last line (see it online).
As noticed in comments, you don't even need operator !=, due to aforementioned behaviour C++20 compiler will automatically convert any such call to operator ==.
I have what is essentially class containing a std::map where the values are shared_ptrs wrapping a container which holds different types. Skeleton code follows:
// Just a basic example class
class MyClass {
public:
explicit MyClass(int i) : mI(i) {}
bool operator==(const MyClass& rhs) { return mI == rhs.mI; }
private:
int mI;
};
// A class into which key value pairs can be added where the value
// can be of a different type.
class MultipleTypeMap {
public:
template <typename T>
void AddObject(const std::string& key, const T object) {
auto ptr = make_shared<B<MyClass>>(std::move(object));
mSharedPtrMap.insert(pair<string, shared_ptr<A>>("key", ptr));
}
// ...
private:
class A {
public:
virtual ~A() = default;
};
template<typename T>
class B : public A {
public:
explicit B(const T& t) : item(t) {}
const T item;
};
map<string, shared_ptr<A>> mSharedPtrMap;
};
int main() {
MyClass m(1);
MultipleTypeMap multiMap;
multiMap.AddObject("test", m);
MyClass n(1);
MultipleTypeMap multiMap2;
multiMap2.AddObject("test", n);
if (multiMap == multiMap2) {
cout << "Equal" << endl;
}
return 0;
}
How should a generic == operator of MultipleTypeMap be written so that it compares the contents of mSharedPtrMap by checking that both the lhs and rhs objects have the same number of keys, the same keys and the same objects where same means that the == operator of the keys / objects evaluates to true?
If you type erase (and later on don't know which type you previously stored), then all the functionality must be provided by the base class interface. So, we need a virtual operator== in A that is implemented in each B.
Here is an implementation:
class MultipleTypeMap {
public:
template <typename T>
void AddObject(const std::string& key, T object) {
auto ptr = std::make_unique<B<T>>(std::move(object));
mMap.emplace(key, std::move(ptr));
}
// ...
bool operator==(const MultipleTypeMap& other) const
{
// Sizes must be equal.
if (mMap.size() != other.mMap.size())
return false;
// Sizes are equal, check keys and values in order.
auto itOther = other.mMap.begin();
for (auto it = mMap.begin(); it != mMap.end(); ++it, ++itOther)
{
if (it->first != itOther->first)
return false;
if (*it->second != *itOther->second)
return false;
}
// No differences found.
return true;
}
bool operator!=(const MultipleTypeMap& rhs) const { return !(*this == rhs); }
private:
class A {
public:
virtual ~A() = default;
virtual bool operator==(const A& other) const = 0;
bool operator!=(const A& other) const { return !(*this == other); }
};
template<typename T>
class B : public A
{
public:
explicit B(const T& t) : item(t) {}
bool operator==(const A& other) const override
{
const B<T>* otherB = dynamic_cast<const B<T>*>(&other);
// If the cast fails, types are different.
if (!otherB)
return false;
// Note: The above is probably slow, consider storing (on construction)
// and checking typeids instead.
// Check item equality.
return item == otherB->item;
}
const T item;
};
std::map<std::string, std::unique_ptr<A>> mMap;
};
Demo with tests
Note: I didn't fix every inconsistency in the original code. (Do you want to move or copy-construct your T? Why store const objects when your MyClass comparison operator is not const?)
I just build a mini program to understand how this will work because i need this for something a bit more difficult but i can't make this work.
I think i need to define operator overload but i dont know how because they are two objects of set<set<a>>
If you compile you will see a big error where it notice that he can't compare myset == myset2 and i think it will say same for operator != and =
#include <set>
using namespace std;
class a{
private:
int a_;
public:
int get_a() const{ return a_; }
void set_a(int aux){ a_=aux;}
bool operator < (const a& t) const{
return this->get_a() < t.get_a();
}
};
class b{
private:
set<set<a> > b_;
public:
void set_(set<a> aux){ b_.insert(aux); }
//Overload operators?
};
int main(){
b myset;
b myset2;
set<a> subset1;
set<a> subset2;
a myint;
myint.set_a(1);
subset1.insert(myint);
myint.set_a(2);
subset1.insert(myint);
myint.set_a(3);
subset1.insert(myint);
myint.set_a(5);
subset2.insert(myint);
myint.set_a(6);
subset2.insert(myint);
myint.set_a(7);
subset2.insert(myint);
myset.set_(subset1);
myset.set_(subset2);
myset2.set_(subset1);
myset2.set_(subset2);
if(myset == myset2){
cout << "They are equal" << endl;
}
if(myset != myset2){
cout << "They are different" << endl;
}
b myset3;
myset3 = myset2; //Copy one into other
}
In order for your code to work you need to specify following operators (note: they are not created by default)
class a{
private:
int a_;
public:
int get_a() const{ return a_; }
void set_a(int aux){ a_=aux;}
/* needed for set insertion */
bool operator < (const a& other) const {
return this->get_a() < other.get_a();
}
/* needed for set comparison */
bool operator == (const a& other) const {
return this->get_a() == other.get_a();
}
};
class b{
private:
set<set<a> > b_;
public:
void set_(set<a> aux){ b_.insert(aux); }
/* needed, because myset == myset2 is called later in the code */
bool operator == (const b& other) const {
return this->b_ == other.b_;
}
/* needed, because myset != myset2 is called later in the code */
bool operator != (const b& other) const {
return !(*this == other);
}
};
You should also take a look at http://en.cppreference.com/w/cpp/container/set and see what other operators std::set uses internally on its elements.
No operator (except for the default operator=(const T&) and operator=(T&&)) is generated by the compiler by default. You should define them explicitly:
class b{
private:
set<set<a> > b_;
public:
void set_(set<a> aux){ b_.insert(aux); }
//Overload operators?
bool operator==(const b& other) const {
return b_ == other.b_;
}
bool operator!=(const b& other) const {
return b_ != other.b_;
}
};
However, this only does not solve the case. Although comparison operators are already defined for std::set<T>, they only work if there are operators for T. So, in this case, you have to define operator== and operator!= for your a class in the same manner as I showed you with b class.
I came up with the following code to implement C#-style properties in C++. It's basically a wrapper class around typename T, with user-defined get/set functions:
#include <functional>
template <typename T>
struct property
{
property(std::function<T(void)> getter, std::function<void(const T&)> setter)
: get(getter), set(setter) { }
property(std::function<T(void)> _getter)
: get(_getter), set([](const T&) { throw std::exception(); }) { }
property<T>& operator=(const T& value) { set(value); return *this; } // w i h o s h r ?
T operator=(const T& value) { set(value); return get(); } // h c t u e e e
operator T() { return get(); }
property<T>& operator=(property<T>& value) { set(value); return *this; }; //...
T operator=(property<T>& value) { set(value); return get(); }; //...
property<T>& operator=(property<T>&) = delete;
// arithmetic / assignment
#define OP(__OP__) \
T operator __OP__(const T& rhs) { return get() __OP__ rhs; } \
T operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return get(); } //...
//property<T>& operator __OP__##= (const T& rhs) { set(get() __OP__ rhs); return *this; } //...
OP(+); OP(-); OP(*); OP(/); OP(%); OP(^); OP(&); OP(|);
#undef OP
// bit shift
#define OP(__OP__) \
T operator __OP__(int rhs) { return get() __OP__ rhs; } \
property<T>& operator __OP__##= (int rhs) { set(get() __OP__ rhs); return *this; }
OP(<<); OP(>>);
#undef OP
// comparison / logic
#define OP(__OP__) bool operator __OP__(const T& rhs) { return get() __OP__ rhs;}
OP(<); OP(>); OP(==); OP(!=); OP(<=); OP(>=); OP(&&); OP(||);
#undef OP
// inc/dec
#define OP(__OP__) \
property<T>& operator __OP__##__OP__() { set(get() __OP__ 1); return *this; } \
T operator __OP__##__OP__(int) { T value = get(); operator __OP__##__OP__(); return value; }
OP(+); OP(-);
#undef OP
T operator ~() { return ~get(); }
bool operator !() { return !get(); }
private:
std::function<T(void)> get;
std::function<void(const T&)> set;
};
struct test
{
property<int> a = property<int>([&]()
{
return x/10;
},
[&](int value)
{
x = value*10;
});
property<int> b = property<int>([&]()
{
return y+10;
},
[&](int value)
{
y = value-10;
});
private:
int x, y;
};
using namespace std;
void main()
{
test x;
x.a = 5;
x.a = x.a + 15;
x.a *= 5;
x.b = x.a;
x.b += x.a / 10;
cout << "property a=" << dec << x.a << endl; // should be 100
cout << "property b=" << dec << x.b << endl; // should be 110
}
My main question here is, should the assignment operators return a T or a property<T>&? Returning a reference seems nicer, yet I want to avoid accidentally reassigning the entire class (including the get/set functions), although I believe overloading operator=(property<T>&) and deleting the copy constructor sufficiently guards against this.
Anyway, both seem to produce the same results, so is there any difference between the two? Which would you prefer and why?
Edit, related question: in the case where a set function is not defined, is there some way to detect assignment at compile time, instead of throwing a run-time exception?
In most cases, returning by copy will work okay, but there are some perverse situations where it won't. Consider the following:
struct A {
A(int i) : i_{i} {}
int i_;
A operator=(A const& A) { i_ = a.i_; return *this; }
};
And then try this:
A a1{1};
A a2{2};
A a3{3};
(a3 = a2) = a1; // What is the value of a3::i_?
Returning by copy means that the a3 = a2 assignment generates a temporary that is reassigned to equal a1. In other words a3::i_ == 2 after the assignment. If you return by reference, a3::i_ == 1.
im new to C++, although i do know the general C syntax. I've been trying to create a class with operator overloading. But i can't get it to work. Well partially got it to work.
Working operator overloading in same source:
//test.cpp
#include <iostream>
class Fraction
{
int gcd(int a, int b) {return b==0 ? a : gcd(b,a%b); }
int n, d;
public:
Fraction(int n, int d = 1) : n(n/gcd(n,d)), d(d/gcd(n,d)) {}
int num() const { return n; }
int den() const { return d; }
Fraction& operator*=(const Fraction& rhs) {
int new_n = n*rhs.n / gcd(n*rhs.n, d*rhs.d);
d = d*rhs.d / gcd(n*rhs.n, d*rhs.d);
n = new_n;
return *this;
}
};
std::ostream& operator<<(std::ostream& out, const Fraction& f){
return out << f.num() << '/' << f.den() ;
}
bool operator==(const Fraction& lhs, const Fraction& rhs) {
return lhs.num() == rhs.num() && lhs.den() == rhs.den();
}
bool operator!=(const Fraction& lhs, const Fraction& rhs) {
return !(lhs == rhs);
}
Fraction operator*(Fraction lhs, const Fraction& rhs)
{
return lhs *= rhs;
}
int main()
{
Fraction f1(3,8), f2(1,2), f3(10,2);
std::cout << f1 << '*' << f2 << '=' << f1*f2 << '\n'
<< f2 << '*' << f3 << '=' << f2*f3 << '\n'
<< 2 << '*' << f1 << '=' << 2 *f1 << '\n';
}
Output:
3/8*1/2=3/16
1/2*5/1=5/2
2*3/8=3/4
Source: http://en.cppreference.com/w/cpp/language/operators
Now my code, trying to apply the code from above
//vectors.h
class Vector2
{
public:
Vector2(void);
~Vector2(void);
int counter;
Vector2& operator+=(const Vector2& vec);
}
//vectors.cpp
#include "vectors.h"
Vector2::Vector2(void)
{
counter = 0;
}
Vector2::~Vector2(void)
{
}
Vector2& operator+=(Vector2& vec)//error: too few parameters
{
int new_n = counter + vec.counter;
counter = new_n;
return *this;//error: this may only be used in a non-static member function.
}
//main.cpp
#include <stdio.h>
#include "vectors.h"
int main(void)
{
Vector2 vector();
while(true)
{
vector += vector;//error: expression must be a modifiable value
printf("Vector counter: %d\n",vector.counter);
}
}
What i'm trying to do:
I'm trying to make my own class, and use operator overloading. But the part i can't get to work is defining the class with a header while keeping operator overloading working.
Thanks for reading my question
The following compiled in ideone: http://ideone.com/ratVVT
Changes are:
Implementation of (overload) method must specify the Class name
Implementation of (overload) method must have the same signature as the declaration (missing const).
Declaring the variable vector in main Vector2 vector(); was interpreted as a function declaration, instead of a Vector2 variable.... use Vector2 vector; or Vector2 vector=Vector2()
The code copied below.
#include <iostream>
//vectors.h
class Vector2
{
public:
Vector2();
~Vector2();
int counter;
Vector2& operator+=(const Vector2& vec);
};
//vectors.cpp
//#include "vectors.h"
Vector2::Vector2()
{
counter = 0;
}
Vector2::~Vector2()
{
}
Vector2& Vector2::operator+=(const Vector2& vec)// <---- CHANGE
{
int new_n = counter + vec.counter;
counter = new_n;
return *this;//error: this may only be used in a non-static member function.
}
//main.cpp
#include <stdio.h>
//#include "vectors.h"
int main()
{
Vector2 vector; // <---- CHANGE
while(true)
{
vector += vector;
printf("Vector counter: %d\n",vector.counter);
}
}
You are missing the class name in your method definition Vector2:: and the signature does not match because of the first parameter missing a const.
Vector2& Vector2::operator+=(const Vector2& vec)