Overload operator < in super class using pure virtual methods - c++

I have a hierarchy of Figure as base class and several sub classes Circle, Square, ecc. I want to overload < operator in Figure to sort Figures by eg. Surface which is implemented in base classes.
This is my code:
//Figure.hpp
class Figure
{
public:
virtual float surface() const = 0;
virtual float perimeter() const = 0;
friend bool operator<(const Figure& lhs, const Figure& rhs);
friend bool operator>=(const Figure& lhs, const Figure& rhs);
}
//Figure.cpp
bool operator<(const Figure& lhs, const Figure& rhs)
{
return lhs.surface() < rhs.surface();
}
bool operator>=(const Figure& lhs, const Figure& rhs)
{
return lhs.surface() >= rhs.surface();
}
//Square.hpp
class Square : public Figure{
public:
Square(float size);
float surface() const{
return mSize * mSize;
};
float perimeter()const{
return mSize * 4;
}
private:
float mSize;
};
Problem is I get an error at runtime:
libc++abi.dylib: Pure virtual function called!
at lhs.surface().
I call the < operator in a LinkedList with Template:
template <typename T>
void List<T>::insertNewNode(T& dataIn)
{
if(isEmpty())
{
insertBegin(dataIn);
}else //otherwise
{
if(dataIn < *startPtr->data)
{
insertBegin(dataIn);
}
else if(dataIn >= *endPtr->data) /
{
insertEnd(dataIn);
}
else
{
//...
}
}
}
//main.cpp
List<Figure> *list = new List<Figure>();
Figure *square = new Square(46);
list->insertNewNode(*square);
EDIT
https://github.com/sanandrea/GenericLinkedList
Is this feasible or I am doing everything wrong?

The problem is both Object Slicing and undefined behavior.
You insert values via
void List<T>::insertNewNode(T& dataIn)
which is OK. Since you pass it by reference the polymorphism will be kept. However, when you later call insertBegin or insertEnd to actually create the node you pass it by T:
void List<T>::insertBegin(T dataIn)
Here dataIn have lost the polymorphism due to Object Slicing. You also have undefined behavior when you store a pointer to the parameter in the newly created node:
ListNode<T> * newPtr = new ListNode<T>(&dataIn); //creates new node
Note that &dataIn is a pointer to the parameter of insertBegin, not a pointer to the dataIn that was passed in insertNewNode(T& dataIn).
You also have Object Slicing in your node: T ListNode<T>::getData() //returns data stored in node.

Related

How do you overload the '<' operator to compare between objects of the same class?

Right now I have a class item
class Item{
public:
short p; //profit
short w; //weight
bool *x; //pointer to original solution variable
void set_values (short p, short w, bool *x);
};
and I need to compare two different instances so that it checks the values of each one and returns either true/false
if (Item a < Item b){
//do something
}
How can I do that? I've been reading cppreference but I don't really understand how to do it.
Very simply,
bool Item::operator<(const Item& other) const {
// Compare profits
return this->p < other.p;
}
To compare both the left hand side p and w with the right hand side p and w use the following code:
class MyClass
{
public:
short p;
short w;
friend bool operator<(const MyClass& lhs, const MyClass& rhs)
{
return lhs.p < rhs.p && lhs.w < rhs.w;
}
};
For example, if you want to compare p, the code should look like this:
class Item {
private:
...
public:
friend bool operator < (const Item& lhs, const Item& rhs) {
return lhs.p < rhs.p;
}
};

Inheritance of overloaded + operator

I have a problem in inheriting overloaded + operator.
Let me make an example.
class Data{
protected:
int data[3];
public:
Data(){
data[0] = data[1] = data[2] = 0;
}
Data operator+(const Data& other)
{
Data temp = *this;
for(int i=0;i<3;i++){
temp.data[i] += other.data[i]
}
return temp;
}
};
class DataInterited:public Data{
public:
};
/******************Main*****************/
DataInterited d1,d2,d3;
d3 = d1 + d2; //=> This is compile error
This code generate compile error saying,
no match for ‘operator=’ (operand types are ‘DataInterited’ and ‘Data’)
I think I have to implement operator+ for DataInherited so that it return DataInherited instance. But in this way, I cannot avoid code duplication.
Is there any way to make d3=d1+d2; line correct while avoiding duplicating the + operator implementation?
There are a couple of things you need to know.
First, always implement operator+ as a free function in terms of operator+=. It saves code duplication and is optimally efficient.
Second, you had no constructor in DataInherited that could take a Data as its argument. This is important because the result of Data::operator+ is a Data, not a DataInherited.
corrected code:
#include <iostream>
#include <algorithm>
class Data{
protected:
int data[3];
public:
Data(){
data[0] = data[1] = data[2] = 0;
}
Data(const Data& other)
{
std::copy(std::begin(other.data), std::end(other.data), data);
}
Data& operator=(const Data& other)
{
std::copy(std::begin(other.data), std::end(other.data), data);
return *this;
}
Data& operator+=(const Data& other)
{
for(int i=0;i<3;i++){
data[i] += other.data[i];
}
return *this;
}
};
Data operator+(Data left, const Data& right)
{
return left += right;
}
class DataInterited:public Data{
public:
DataInterited(Data d = {})
: Data(std::move(d))
{}
};
using namespace std;
auto main() -> int
{
DataInterited d1,d2,d3;
d3 = d1 + d2; //=> This is no longer a compile error
return 0;
}
Koenig operator forwarding to an increment_by function.
Derived classes can implement their own increment_by overloads if they want different behavior.
SFINAE stuff skipped, so bad types will give hard errors.
class Data{
public:
template<class D, class Rhs>
friend D operator+=(D&& lhs, Rhs&& rhs){
increment_by(lhs,std::forward<Rhs>(rhs));
return std::forward<D>(lhs);
}
template<class Lhs, class Rhs>
friend Lhs operator+(Lhs lhs, Rhs&& rhs){
lhs+=std::forward<Rhs>(rhs);
return std::move(lhs);
}
friend void increment_by(Data& self, Data const&other){
for(int i=0;i<6;i++){
self.data[i] += other.data[i];
}
}
};
Both + and += are template friends and hence the types passed can be derived classes. So type isn't lost,
increment_by needs overiding if derived type needs new behaviour. If not, leave it alone.
live example.
Do not leave the type needlessly. Converting from base to derived basically throws out the point of the derived type.

Overloading a bool operator with a member function

I have a class like this:
class AI
{
private:
struct Comparator
{
bool operator()(const Town* lfs, const Town* rhs)
{
return GetHeuristicCost(lfs) > GetHeuristicCost(rhs);
}
};
int GetHeuristicCost(const Town* town);
// constructor and variables
};
GetHeuristicCost returns the heuristic from the town parameter to the exit of the path.
What I am trying to do is overload the bool operator for a priority queue but it gives me the error
a nonstatic member reference must be relative to a specific object
I know why it is giving me this error but what I don't know is how to use a nonstatic function inside the Comparator struct.
GetHeuristicCost must be nonstatic
I tried moving GetHeuristicCost inside the Town class to no avail
I need to overload the operator with a struct because I need to use two different bool overloadings on the () for two different circumstances but with the same parameters (two Towns). In other words I need the struct so I can't do this:
bool operator()(const Town* lfs, const Town* rhs)
{
return GetHeuristicCost(lfs) > GetHeuristicCost(rhs);
}
Basically I plan on having two structs like this:
struct Comparator1
{
bool operator()(const Town* lfs, const Town* rhs)
{
return GetHeuristicCost(lfs) > GetHeuristicCost(rhs);
}
};
struct Comparator2
{
bool operator()(const Town* lfs, const Town* rhs)
{
return GetHeuristicCost(lfs) + GetTotalCost (lfs, rhs) > GetHeuristicCost(rhs) + GetTotalCost (lfs, rhs);
}
};
You need to construct instances of the Comparator nested class with a pointer/reference to their "outer" class instance.
class AI
{
private:
struct Comparator
{
const AI &outer;
Comparator(const AI &o):outer(o){}
bool operator()(const Town* lfs, const Town* rhs)const
{
return outer.GetHeuristicCost(lfs) > outer.GetHeuristicCost(rhs);
}
};
int GetHeuristicCost(const Town* town)const;
};
// how to use in code:
AI::Comparator comp(*this);
priority_queue<Town*, vector<Town*>, AI::Comparator> priorityQueue(comp);

c++ How to search if struct element in vector is equal?

A class named SparseMatrix has a vector of Nodes struct. I want to overload the += operator so that if the i and j members of a Node instance are the same then the value of that node will be added to This. How can i accomplish this using methods from the algorithm library?
I tried using find_if to pass to a function but it act only on one iterator:
class SparseMatrix
{
public:
SparseMatrix(int numRow,int numCol, std::vector<double> fill);
SparseMatrix(int numRow,int numCol);
SparseMatrix();
// assignment operations
bool operator==(const SparseMatrix &other) const;
bool operator!=(const SparseMatrix &other) const;
void operator-() const;
// compound operations
SparseMatrix& operator+=(const SparseMatrix &other);
SparseMatrix& operator*=(const SparseMatrix &other);
// binary operations
const SparseMatrix operator+(const SparseMatrix &other) const;
const SparseMatrix operator*(const SparseMatrix &other) const;
friend std::ostream& operator<<(std::ostream& output, const SparseMatrix sparseMatrix);
bool trace(double& result) const;
bool det(double& result) const;
SparseMatrix transpose();
~SparseMatrix(){};
protected:
vector<Node> _matrix;
int _numCol, _numRow;
};
typedef struct Node {
int i;
int j;
double value;
static bool samePosition(const Node& other)
{
return ((i == other.i) && (j == other.j));
}
} Node;
SparseMatrix& SparseMatrix::operator+=(const SparseMatrix &other)
{
vector<Node>::iterator itThis;
for (vector<Node>::iterator itOther = other._matrix.begin(); itOther != other._matrix.end(); ++itOther)
{
// find if already exists a value in the same matrix position
itThis = find_if(_matrix.begin(), _matrix.end(), Node::samePosition);
// if exists add value to position, else instantiate new Node with value & position
}
return *this;
}
Basically, I want Node::samePosition() to pass two parameters - the current iterator passed by find_if and itOther so it can check if they are equal.
EDIT: I have separated the samePosition function and now want to pass to it two parameters using find_if:
typedef struct Node {
int i;
int j;
double value;
} Node;
static bool SparseMatrix::samePosition(const Node& first, const Node& other)
{
return ((first.i == other.i) && (first.j == other.j));
}
SparseMatrix& SparseMatrix::operator+=(const SparseMatrix &other)
{
vector<Node>::iterator itThis;
for (vector<Node>::iterator itOther = other._matrix.begin(); itOther != other._matrix.end(); ++itOther)
{
itThis = find_if(_matrix.begin(), _matrix.end(), SparseMatrix::samePosition("call what here?",itOther));
}
return *this;
}
You are trying to use
static bool SparseMatrix::samePosition(const Node& first, const Node& other)
{
return ((first.i == other.i) && (first.j == other.j));
}
which is a standalone function. All its data has to be supplied by the caller, but find_if knows nothing about the Node you want to compare against the entire list.
Instead you should use a functor, which is an object that can hold some data, and also implements operator()() so that it can be called like a function.
struct position_finder
{
const Node needle;
position_finder( const Node& sought ) : needle(sought) {}
bool operator()( const Node& haystack ) const
{
return ((needle.i == haystack.i) && (needle.j == haystack.j));
// or return samePosition(needle, haystack)
}
};
and then you pass the sought Node when constructing the functor, so it gets stored for later use:
itThis = find_if(_matrix.begin(), _matrix.end(), position_finder(*itOther));
C++11 makes this all a whole lot easier, since a lambda will cause the compiler to generate that struct for you:
itThis = find_if(_matrix.begin(), _matrix.end(), [itOther](Node& arg){ return ((itOther->i == arg.i) && (itOther->j == arg.j)); });

Implementing operator< on abstract base class

I have a type hierarchy, and I'm not sure of a clean / good way to implement operator< and operator==.
Essentially, I already have this:
class Parent {
public:
virtual ~Parent() {}
};
class A : public Parent { int data; };
class B : public Parent { double data; };
class C : public Parent { std::string data; };
bool operator==(A const & lhs, A const & rhs) { return lhs.data == rhs.data; }
bool operator< (A const & lhs, A const & rhs) { return lhs.data < rhs.data; }
bool operator==(B const & lhs, B const & rhs) { return lhs.data == rhs.data; }
bool operator< (B const & lhs, B const & rhs) { return lhs.data < rhs.data; }
bool operator==(C const & lhs, C const & rhs) { return lhs.data == rhs.data; }
bool operator< (C const & lhs, C const & rhs) { return lhs.data < rhs.data; }
What I'd like to implement as well, is this:
bool operator==(Parent const & lhs, Parent const & rhs) { ... }
bool operator< (Parent const & lhs, Parent const & rhs) { ... }
I've currently implemented it by doing:
bool operator==(Parent const & lhs, Parent const & rhs) {
try {
return dynamic_cast<A const &>(lhs) == dynamic_cast<A const &>(rhs);
} catch(std::bad_cast const & e) {
}
try {
return dynamic_cast<B const &>(lhs) == dynamic_cast<B const &>(rhs);
} catch(std::bad_cast const & e) {
}
try {
return dynamic_cast<C const &>(lhs) == dynamic_cast<C const &>(rhs);
} catch(std::bad_cast const & e) {
}
assert(typeid(lhs) != typeid(rhs));
return false;
}
But this just seems awful. Is there a cleaner way of going about this?
For comparisons of complex types, you may find Double Dispatch useful.
If your types are very simple, it is sometimes effective to roll them all into one. In the example of 3 unsigned variants, it would likely be better to just use one type to accommodate all sizes, and to avoid dynamic dispatch and more complicated graphs of types.
Applied to original question; where A, B, and C all used unsigned types:
well, one quick and dirty approach would be:
class Parent {
protected:
virtual ~Parent() {}
public:
bool operator<(const Parent& pOther) const {
return this->as_uint64() < pOther.as_uint64();
}
// ...
private:
// using a type which accommodates all values
virtual uint64_t as_uint64() const = 0;
};
and then deriving from Parent would take the form:
class A : public Parent {
// ...
private:
virtual uint64_t as_uint64() const { return this->data; }
private:
uint16_t data;
};
then Parent could simply define all comparators, and all Parent types would be comparable.
Use a virtual comparator for single dispatch and dynamic_cast for type casting:
class ABC_base {
public:
virtual ~ABC_base() {}
bool operator < (ABC_base const & rhs) const {
return this->comparator(rhs) < 0;
}
protected:
virtual int comparator (ABC_base const &) = 0;
};
class ABC : public ABC_base {
protected:
virtual int comparator(ABC_base const & rhs) const {
try {
return my_comparator(dynamic_cast<ABC const&>(rhs));
// Run-time cast failed - use double dispatch as fallback
} catch (std::bad_cast&) {
return -rhs.comparator(*this);
}
}
private:
int my_comparator(ABC const & rhs) const {
if (data < rhs.data)
return -1;
if (data == rhs.data)
return 0;
if (data > rhs.data)
return 1;
}
T data;
};
Here's how the code works:
The base class's operator < is called, which uses dynamic lookup to find the comparator. It checks the returned value to see if it's lesser.
The derived class's comparator attempts to downcast the base class reference so that comparison can be done on the derived class's members.
Why the base class reference, instead of using the derived class reference?
Virtual dispatch would not work otherwise due to incorrect function signature.
Should the downcast succeed, it calls the non-virtual private comparator. Otherwise, it uses virtual dispatch again to do (rhs ? *this) and negates the result to compensate for the inverted ordering.
Why not have the cast and comparison in the one virtual function? It will make the code messier since the function will do two things: casting and comparing. Hence, there's a private comparator function. Should you want to use the base function in a derived class, along the lines of class ABC_der : public ABC, call ABC::comparator(static_cast<ABC const&>(rhs)). The use of Base:: forces static dispatch so you don't have to expose the helper comparison function.
Right now, this and rhs are of the same type, so we can finally do the actual comparison. A chain of if statements is used to return a value conformant to Java's Comparable and C's qsort() semantics.