Why does bool casting is called? - c++

Why is bool casting being called ?
The problem appears when the constructor Set result(*this) is called. I expect it to use the Copy Constructor, instead it casts *this to bool and uses it as an int for the constructor.
How to fix it to use the copy constructor?
Set Set::operator+(const Set& rhs)const
{
Set result(*this);
for (unsigned int i = 0; i < rhs.getSize(); i++)
{
result.add(rhs[i]);
}
return result;
}
Set::operator bool()const
{
return !!(*this);
}
Set::Set(size_t capacity)
{
data = new int[capacity];
size = 0;
this->capacity = capacity;
}
void Set::copy(const Set& copied)
{
size = copied.getSize();
capacity = copied.getCapacity();
if (data != nullptr)
delete[]data;
data = new int[capacity];
for (unsigned int i = 0; i < size; i++)
data[i] = copied.getAt(i);
}
Set::Set(Set& copied)
{
copy(copied);
}
Set& Set::operator=(const Set& copied)
{
if (this != &copied)
copy(copied);
return *this;
}
int& Set::getAt(unsigned int idx)const
{
if (idx < 0 || idx >= size)
throw "Invalid index\n";
return data[idx];
}
bool Set::operator !()const
{
if (size == 0)
return true;
return false;
}

The argument for your copy constructor Set::Set(Set& copied) is not a const reference. The operator Set Set::operator+(const Set& rhs)const is const so this is a const Set * and *this is a const Set. Since you cannot pass a const T to a T& argument (it would discard the const) you may not make use of the copy constructor in this context.
To fix this, make your copy constructor accept const Set & instead, just like the copy member function does :
Set::Set(const Set& copied)
// ^^^^^ Added const here
{
copy(copied);
}
Edit : Mandatory disclaimer that you should not have to write your own dynamically sized array. Use std::vector instead. It will greatly simplify your type and will probably be far safer.

You can declare casting operator explicit to avoid implicit casting to bool:
explicit operator bool() const {
...
}
And then use it like this:
Set::bool(//obj_name);

Related

Problem with my Copy and Move constructor and assignment operators

I want to build my own full Vector class in C++. I started like this:
#include <iostream>
#include <initializer_list>
#define Print(x)(std::cout<< x << std::endl)
// Vector Class //
template <typename T>
class Vector
{
// Attributes
int length = 0;
T* array;
public:
// Default constructor
Vector()
: length(0), array(nullptr)
{ }
// Copy constructor
template<typename U>
Vector(const Vector<U>& other)
: Vector(other.len())
{
Print("Use Copy Constructor");
// Coppying other data to new array
array = new T[other.len()];
for (auto i=0; i < other.len(); i++)
array[i] = other[i];
}
// Move constructor
Vector(Vector<T>&& other)
: length(other.len()), array(other.array)
{
Print("Use Move Constructor");
// Deleting other array
other.length = 0;
other.array = nullptr;
}
// Constructor (does not allow uniform initialisation)
Vector(int length)
: length(length), array(new T[length])
{ }
// Constructor (with initialiser list and delegate constructor)
Vector(std::initializer_list<T> list)
: Vector((int)list.size())
{
std::uninitialized_copy(list.begin(), list.end(), array);
}
// Destructor
~Vector()
{
length = 0;
delete[] array;
array = nullptr;
}
// Function Len that returns the length
int len() const
{
return length;
}
// Operator[](int i) and its const overload
auto& operator[](int i)
{
return array[i];
}
auto& operator[](int i) const
{
return array[i];
}
// Copy assignment operator
template<typename U>
Vector& operator=(const Vector<U>& other)
{
Print("Use Copy Operator");
if (this != (Vector*)& other) {
/*
This works the same way but does not solve the problem:
Vector<typename std::common_type<T,U>::type> temp(other);
temp.swap(*this);
*/
delete[] array;
length = other.len();
array = new T[other.len()];
for (auto i = 0; i < other.len(); i++)
array[i] = other[i];
}
return *this;
}
// Move assignment opertator
Vector& operator=(Vector<T>&& other)
{
Print("Use Move Operator");
if (this != (Vector*)&other){
/*
This works the same way but does not solve the problem:
Vector<T> temp(std::move(other)); // moves the array
temp.swap(*this);
*/
delete[] array;
length = other.len();
array = other.array;
other.len() = 0;
other.array = nullptr;
}
return *this;
}
};
But if I now try to use the copy assignment operator like this:
Vector<double> double_vector = {3.4677, 3.212, 2.2, 6.3};
Vector<double> a = double_vector;
I get the following error message:
error: use of deleted function 'constexpr Vector<double>::Vector(const Vector<double>&)'
I assume that the problem lies within the copy constructor and copy assignment operator, but I sadly can't find a solution. What I find strange is that, if I comment out the move constructor and move assignment operator, the code does seem to work. This lets me think that the compiler has difficulty knowing which constructor to use. But I could also be very wrong about this.
Hope I gave enough info for an answer/push in the right direction.
A template is never a copy constructor, that is a converting constructor.
And as you have defined a bunch of other constructors, the otherwise default copy constructor will be defined as deleted.

C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

I have created a Vector class that holds 3 parameters and overloaded the -= operator
template <class T>
class Static3Vector
{
public:
Static3Vector() : m_coords{ 0, 0, 0 } {}
Static3Vector(T x, T y, T z) : m_coords{ x, y, z } {}
T operator [] (const size_t& i) { return m_coords[i]; }
T operator [] (const size_t& i) const { return m_coords[i]; }
Static3Vector operator -= (const Static3Vector& rhs)
{
for (int i = 0; i < sizeof(m_coords) / sizeof(m_coords[0]); i++)
this[i] -= rhs[i];
return *this;
}
private:
T m_coords[3];
};
but when I try using this operator
Static3Vector<int> vec1(1,2,3);
Static3Vector<int> vec2(1,2,3);
vec1 -= vec2;
I get the error I typed out in the title.
this is a pointer and this[i] is not doing what you expect. It takes this and performs pointer arithmetic. The only value of i that is not UB is then 0 and type of this expression is Static3Vector<T>
It is pretty much equivalent to this:
Static3Vector<int> vec1(1,2,3);
Static3Vector<int>* ptr = &vec1;
ptr[0]; //ok, equivalent to *ptr
ptr[1]; //compiles, but invokes UB
You can solve it in a couple of ways:
(*this)[i] -= rhs[i]; //dereference first
this->operator[](i) -= rhs[i]; //call operator explicitly
m_coords[i] -= rhs[i]; //don't use overloaded operator, use data directly
One more thing: First two will not work with your current implementation, because your operator[] returns a copy of the value it stores. It's more typical for operator[] to return a reference to stored object:
T& operator [] (const size_t& i) { return m_coords[i]; }
const T& operator [] (const size_t& i) const { return m_coords[i]; }
See also What are the basic rules and idioms for operator overloading?
Take a look at your indexing-operators:
T operator [] (const size_t& i) { return m_coords[i]; }
T operator [] (const size_t& i) const { return m_coords[i]; }
The only difference between them is that one is called for constant objects, begging the question why you duplicated them at all.
That is easily answered though:
Both should return references, the latter const-qualified, to avoid copies and allow modification for the first one.
Fix that as a first step to resolving your error.
As a second step, recognize that this was introduced before references existed, and is thus a pointer to the instance, not a reference, thus this[n] is pointer-arithmetic, not calling your custom indexing-operator. Thankfully that results in a different type and thus failed overload-resolution.
there is incorrect use of the subscript operator. In the operator definitions as for example in this
Static3Vector operator -= (const Static3Vector& rhs)
{
for (size_t i = 0; i < sizeof(m_coords) / sizeof(m_coords[0]); i++)
this[i] -= rhs[i];
return *this;
}
in this statement
this[i] -= rhs[i];
there is used the b built-in subscript operator for an object of the pointer type Static3Vector *.
You need to write
( * this )[i] -= rhs[i];
or
this->operator[]( i ) -= rhs[i];
Also the return type shall be a referenced type like
T & operator [] (const size_t& i) { return m_coords[i]; }
const T & operator [] (const size_t& i) const { return m_coords[i]; }
and
Static3Vector & operator -= (const Static3Vector& rhs)
{
for (size_t i = 0; i < sizeof(m_coords) / sizeof(m_coords[0]); i++)
( *this )[i] -= rhs[i];
return *this;
}

Copy Constructor (Deep Copy) c++

I am working on an assignment that calls for the writing the function implementations of the Big 3 when constructing a class with pointers. I seem to be having trouble with the copy constructor. I think I am doing the other 2 correctly.
class RealBox
{
private:
float* m_reals; // Array of Real Numbers
int m_boxsize; // number of Real Numbers in this box
public:
// Purpose: Constructs an Real-Box
// Preconditions:
// 's' is greater than 0;
// Postconditions:
// m_reals points to a dynamically allocated array of size 's'
// all elements of m_reals[] are initiallized to 'a'.
RealBox(int s, float a);
/*
* --------- Big 3 Member Functions -----------
*/
// Purpose: Destructor
// Postconditions: m_reals[] deallocated
~RealBox();
// Purpose: Operator=, performs a deep copy of 'rhs' into 'this' RealBox
// Parameters: rhs, RealBox to be copied
// Returns: *this
// Postconditions: *this == rhs
const RealBox& operator=(const RealBox& rhs);
// Purpose: Copy Constructor
// Parameters: rhs - RealBox to be copied
// Postconditions: *this == rhs
RealBox(const RealBox& rhs);
/*
* ----- Simple Accessor Operations -----
*/
// Purpose: Sets a value in the RealBox
// Parameters: 'i' location to set
// 'x' value to store
// PreConditions: 'i' is between the boundaries of the RealBox
// Postconditions: element 'i' in the RealBox is set to 'x'
void set( int i, float x);
/*
* ----- Complex Accessor Operations -----
*/
// Purpose: prints the RealBox
friend std::ostream& operator<< (std::ostream& out,
const RealBox& box);
}; // RealBox
#include "realbox.h"
#include <iostream>
using namespace std;
RealBox::RealBox(int s, float a)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
{
m_reals[i] = a;
}
}
RealBox::~RealBox()
{
delete [] m_reals;
m_reals = NULL;
}
const RealBox& RealBox::operator =(const RealBox& rhs)
{
if(this != &rhs)
*this = rhs;
return(*this);
}
RealBox::RealBox(const RealBox& rhs)
{
m_reals = new float[m_boxsize];
*this = rhs.m_reals;
}
void RealBox::set(int i, float x)
{
m_reals[i] = x;
}
std::ostream& operator<< (std::ostream& out, const RealBox& box)
{
out <<"[ ";
for (int i = 0; i < box.m_boxsize; i++)
{
out << box.m_reals[i] << ", ";
}
out <<"]"<< endl;
return(out);
}
When I try to compile, I am getting the error:
realbox.cpp: In copy constructor ‘RealBox::RealBox(const RealBox&)’:
realbox.cpp:33:8: error: no match for ‘operator=’ (operand types are ‘RealBox’ and ‘float* const’)
*this = rhs.m_reals;
^
realbox.cpp:33:8: note: candidate is:
realbox.cpp:23:16: note: const RealBox& RealBox::operator=(const RealBox&)
const RealBox& RealBox::operator =(const RealBox& rhs)
^
realbox.cpp:23:16: note: no known conversion for argument 1 from ‘float* const’ to ‘const RealBox&’
I think my operator = overload is faulty as well but I'm not sure exactly what went wrong. It didn't give me that error until I started trying to fix. It's trying to say I'm comparing two different data types but I'm not sure how that is happening.
This line:
*this = rhs.m_reals;
Attempts to assign a float* to a RealBox object. The two types are incompatible.
Second, you failed to initialize m_boxsize when you constructed the object.
What you want to do is this:
RealBox::RealBox(const RealBox& rhs)
{
m_boxsize = rhs.boxsize;
m_reals = new float[m_boxsize];
for (int i = 0; i < m_boxsize; ++i )
m_reals[i] = rhs.m_reals[i];
}
This can be shortened to this:
RealBox::RealBox(const RealBox& rhs) : m_boxsize(rhs.m_boxsize),
m_reals(new float[rhs.m_boxsize])
{
std::copy(rhs.m_boxsize, rhs.m_boxsize + m_boxsize, m_reals);
}
In addition, your other constructor needs to initialize the m_boxsize variable:
RealBox::RealBox(int s, float a) : m_boxsize(s)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
m_reals[i] = a;
}
This can also be shortened to this:
RealBox::RealBox(int s, float a) : m_boxsize(s), m_reals(new float[s])
{
std::fill( m_reals, m_reals + s, a);
}
The std::fill and std::copy are defined in the <algorithm> header.
Once you fix this, the assignment operator is very simple:
#include <algorithm>
//...
RealBox& RealBox::operator =(RealBox rhs)
{
std::swap(rhs.m_reals, m_reals);
std::swap(rhs.m_boxsize, m_boxsize);
return *this;
}
Usage of the copy/swap idiom makes this all possible. Simple as simple can be.
What is the copy-and-swap idiom?
sorry for short answer ...
first add a member to save the table size inside the costructor
RealBox::RealBox(int s, float a)
{
m_reals = new float[s];
for (int i=0; i < s; i++)
{
m_reals[i] = a;
}
m_realCount = s;
}
then replace :
RealBox::RealBox(const RealBox& rhs)
{
m_reals = new float[m_boxsize];
*this = rhs.m_reals; // << this is a big mistake
}
by :
RealBox::RealBox(const RealBox& rhs)
{
this->m_reals = new float[rhs.m_realCount];
memcpy(this->m_reals,rhs.m_reals,rhs.m_realCount*sizeof(float));
this->m_realCount = rhs.m_realCount;
}
You may need to include stdlib.h

Operator overloading "equal to"

I want to overload equal to "=" operator in C++ for
class Array
{
int *p;
int len;
};
All functions/constructor etc. are defined.
My question:
Could someone give me the prototype of the operator overloaded function?
And suppose:
Array a,b;
b=a;
Which of "a" and "b" would be passed implicitly and which explicitly?
Thanks in advance.
The prototype is Array& operator=(const Array& that).
While implementing this, remember about the rule of three and make good use of the copy-and-swap idiom.
You're looking for the assignment operator = (not equal-to, which is operator== and usually serves as an equality comparison)
class Array
{
int *p;
int len;
public:
// Assignment operator, remember that there's an implicit 'this' parameter
Array& operator=(const Array& array)
{
// Do whatever you want
std::cout << "assignment called";
return *this;
}
};
int main(void) {
Array a, b;
a = b;
}
remember that since you wrote "All functions/constructor etc. are defined" you should pay attention to what you need your class to do and possibly also implement destructor as in the rule of three (and/or take a look at its variants in C++11, might be relevant since there's no other code posted).
There is probably more than one way to do it, but here is an option.
Public Functions:
Array::Array(const Array& array)
{
Allocate(0);
*this = array;
}
Array::~Array()
{
Deallocate();
}
const Array& Array::operator=(const Array& array)
{
if (this == &array)
return *this;
Deallocate();
Allocate(array.len);
for (int i=0; i<len; i++)
p[i] = array.p[i];
return *this;
}
Non-Public Functions:
void Array::Allocate(int size)
{
len = size;
if (len > 0)
p = new int[len];
}
void Array::Deallocate()
{
if (len > 0)
delete[] p;
len = 0;
}
Of course, you can always use a vector<int> instead...

How do I go about overloading C++ operators to allow for chaining?

I, like so many programmers before me, am tearing my hair out writing the right-of-passage-matrix-class-in-C++. I have never done very serious operator overloading and this is causing issues. Essentially, by stepping through
This is what I call to cause the problems.
cMatrix Kev = CT::cMatrix::GetUnitMatrix(4, true);
Kev *= 4.0f;
cMatrix Baz = Kev;
Kev = Kev+Baz; //HERE!
What seems to be happening according to the debugger is that Kev and Baz are added but then the value is lost and when it comes to reassigning to Kev, the memory is just its default dodgy values. How do I overload my operators to allow for this statement?
My (stripped down) code is below.
//header
class cMatrix
{
private:
float* _internal;
UInt32 _r;
UInt32 _c;
bool _zeroindexed;
//fast, assumes zero index, no safety checks
float cMatrix::_getelement(UInt32 r, UInt32 c)
{
return _internal[(r*this->_c)+c];
}
void cMatrix::_setelement(UInt32 r, UInt32 c, float Value)
{
_internal[(r*this->_c)+c] = Value;
}
public:
cMatrix(UInt32 r, UInt32 c, bool IsZeroIndexed);
cMatrix( cMatrix& m);
~cMatrix(void);
//operators
cMatrix& operator + (cMatrix m);
cMatrix& operator += (cMatrix m);
cMatrix& operator = (const cMatrix &m);
};
//stripped source file
cMatrix::cMatrix(cMatrix& m)
{
_r = m._r;
_c = m._c;
_zeroindexed = m._zeroindexed;
_internal = new float[_r*_c];
UInt32 size = GetElementCount();
for (UInt32 i = 0; i < size; i++)
{
_internal[i] = m._internal[i];
}
}
cMatrix::~cMatrix(void)
{
delete[] _internal;
}
cMatrix& cMatrix::operator+(cMatrix m)
{
return cMatrix(*this) += m;
}
cMatrix& cMatrix::operator*(float f)
{
return cMatrix(*this) *= f;
}
cMatrix& cMatrix::operator*=(float f)
{
UInt32 size = GetElementCount();
for (UInt32 i = 0; i < size; i++)
{
_internal[i] *= f;
}
return *this;
}
cMatrix& cMatrix::operator+=(cMatrix m)
{
if (_c != m._c || _r != m._r)
{
throw new cCTException("Cannot add two matrix classes of different sizes.");
}
if (!(_zeroindexed && m._zeroindexed))
{
throw new cCTException("Zero-Indexed mismatch.");
}
for (UInt32 row = 0; row < _r; row++)
{
for (UInt32 column = 0; column < _c; column++)
{
float Current = _getelement(row, column) + m._getelement(row, column);
_setelement(row, column, Current);
}
}
return *this;
}
cMatrix& cMatrix::operator=(const cMatrix &m)
{
if (this != &m)
{
_r = m._r;
_c = m._c;
_zeroindexed = m._zeroindexed;
delete[] _internal;
_internal = new float[_r*_c];
UInt32 size = GetElementCount();
for (UInt32 i = 0; i < size; i++)
{
_internal[i] = m._internal[i];
}
}
return *this;
}
Your operators + and * must return by value, not by reference. You're returning a temporary variable by reference. Also, you're arguments are passed by value when it should be a const reference:
cMatrix cMatrix::operator+(cMatrix const& m)
{
cMatrix matrix(*this);
matrix += m;
return matrix;
}
cMatrix cMatrix::operator*(float f)
{
cMatrix matrix(*this);
matrix *= m;
return matrix;
}
You should take a look at Boost.Operators. This would let you implement only operator*= and operator+= and automatically provide correct implementations for operator+ and operator*.
PS: If you implement your matrix class just for the learning experience, don't hesitate to look at other implementations like the Matrix Template Library.
PPS: If you don't want to use boost, or if you just want to understand the best practice, take a look at Boost.Operator and do what they do.
IMO the canonical form of overloading addition is this:
class X {
public:
X& operator+=(const X& rhs) { /*add rhs to *this*/ }
};
inline X operator+(X lhs, const X& rhs) {lhs+=rhs; return lhs;}
The same goes for -, *, /, where applicable.
Note that + returns a copy, not a reference. That's important, because A+B creates a new value, so it cannot return a reference to an existing one.
Also, it is a free function. IMO it's best to implement those of the binary operators which can be implement either as a member or as a free function as free functions, if they treat their operands symmetrically (as does +), and as member functions, if they treat their operands asymmetrically (as +=, which changes its left argument. If you implement operator+ as a member, you will have to make the function const (X operator+(const X& rhs) const), so that it can be invoked for constant elements on the left side.