I want to create a bigInteger class that uses arrays in backend.
BigInt a = 4321; // assume in BigInt class: array is {4,3,2,1}
BigInt b = 2131; // assume in BignInt class: array is {2,1,3,1}
BigInt sum = a + b; // array {6,4,5,1}
I wrote this function to overload '+' operator to add the two numbers
int* operator+(const uint32& other) const{
uint32 sum[n];
for(int i=0; i<n; i++){
sum[i] = (*this[i]) + other[i];
}
return sum;
}
but it doesn't work.
Note: assume the array size to be fixed.
My question was not answered in Overload operator '+' to add two arrays in C++ and I made more clarification on this question.
Thank you
The immediate problem is that you're returning a pointer to an automatic (i.e. function-local) variable. The variable goes out of scope the moment the function returns. Dereferencing the returned pointer would lead to undefined behaviour.
I would argue that the method's signature should look like this:
class BigInt {
BigInt operator+(const BigInt& other) const {...}
...
}
In other words, it should take a reference to an instance of BigInt and should return another instance of BigInt (by value).
// friend_artih_op.cpp (cX) 2014 adolfo.dimare#gmail.com
// http://stackoverflow.com/questions/27645319/
/*
I like arithmetic operators to be 'friend' functions instead of 'class
members' because the compiler will automatically insert invocations to
construct a class value from literals, as it happens inside the
'assert()' invocations in this program.
*/
/// A small 'fake' arithmetic class
class arith {
private:
long * m_ptr; ///< pointed value
public:
arith( long val=0 )
{ m_ptr = new long; *m_ptr = val; }; ///< Default constructor.
arith( const arith& o )
{ m_ptr = new long;
*m_ptr = *(o.m_ptr); } ///< Copy constructor.
~arith( ) { if (0!=m_ptr) { delete m_ptr; } } ///< Destructor.
// operator long() const { return *m_ptr; } ///< Get stored value.
long to_long() const { return *m_ptr; } ///< Get stored value.
friend arith operator+( const arith left , const arith right );
friend bool operator==( const arith left , const arith right );
};
inline arith operator+( const arith left , const arith right ) {
long res = *(left.m_ptr) + *(right.m_ptr);
// 'new long' will be called within the copy constructor
return arith( res );
} ///< letf+rigth
inline bool operator==( const arith left , const arith right ) {
return ( *(left.m_ptr) + *(right.m_ptr) );
} ///< letf==rigth ???
#include <cassert> // assert()
int main() {
arith four(4);
assert( arith(6) == 2+four ); // 2 gets converted to arith(2)
assert( (6) == (2+four).to_long() );
// If you make 'operator+()' a member function, you would not have the
// compiler do the conversion (and you have to do it yourself):
assert( arith(6) == arith(2)+four );
}
// EOF: friend_artih_op.cpp
Related
I wanted to return the value of a property of a class using assignment operator. I tried to fulfill this purpose. I searched a lot on the web but all of the websites I visited talked about how to overload assignment operator to do like a copy constructor of a class like this: class_t& operator=(class_t&);. Can anybody help me overload this operator to return the value of a property of a class?
This is my code:
class A_t
{
private:
int value = 0;
public:
int operator = (A_t); // I failed to overload assignment operator for this
A_t& operator = (int); // I succeeded to overload assignment operator for this
int Value();
void setValue(int);
};
A_t& A_t::operator = (int value)
{
this->setValue(value);
return *this;
}
int operator = (A_t &data)
{
return data.value;
}
int A_t::Value() { return this->value; }
void A_t::setValue(int data) { this->value = data; }
int main()
{
A_t object = 3;
int value = object; // Error: cannot convert 'A_t' to 'int' in initialization
cout << value << endl;
return 0;
}
You can’t overload operator = for this. What you can do instead is overload the implicit conversion-to-int operator inside the class:
operator int() const { return value; }
However, think carefully whether this is actually a good idea in your case. Implicit conversions should usually be avoided at all cost, because it’s very error-prone (many smart people think C++ shouldn’t allow defining custom implicit conversions at all!).
For this, you need an int operator for your class which returns the variable when being assigned to an integer. Plus the class missed the constructor required for A_t object = 3;. The corrected class looks like this,
class A_t
{
private:
int value = 0;
public:
//int operator = (A_t); <-- You dont need this.
A_t& operator = (int); // I succeeded to overload assignment operator for this
int Value();
void setValue(int);
/**
* Construct using an integer value.
*
* #param val: The value to be set.
*/
A_t(int val) : value(val) {}
/**
* int operator.
*
* #return The value stored inside.
*/
operator int() const
{
return value;
}
/**
* int& operator (optional).
*
* #return The variable stored inside.
*/
operator int& ()
{
return value;
}
};
A_t& A_t::operator = (int value)
{
this->setValue(value);
return *this;
}
int A_t::Value() { return this->value; }
void A_t::setValue(int data) { this->value = data; }
int main()
{
A_t object = 3;
int value = object; // Error: cannot convert 'A_t' to 'int' in initialization
cout << value << endl;
return 0;
}
Is there a difference between defining a global operator that takes two references for a class and defining a member operator that takes only the right operand?
Global:
class X
{
public:
int value;
};
bool operator==(X& left, X& right)
{
return left.value == right.value;
};
Member:
class X
{
int value;
bool operator==( X& right)
{
return value == right.value;
};
}
One reason to use non-member operators (typically declared as friends) is because the left-hand side is the one that does the operation. Obj::operator+ is fine for:
obj + 2
but for:
2 + obj
it won't work. For this, you need something like:
class Obj
{
friend Obj operator+(const Obj& lhs, int i);
friend Obj operator+(int i, const Obj& rhs);
};
Obj operator+(const Obj& lhs, int i) { ... }
Obj operator+(int i, const Obj& rhs) { ... }
Your smartest option is to make it a friend function.
As JaredPar mentions, the global implementation cannot access protected and private class members, but there's a problem with the member function too.
C++ will allow implicit conversions of function parameters, but not an implicit conversion of this.
If types exist that can be converted to your X class:
class Y
{
public:
operator X(); // Y objects may be converted to X
};
X x1, x2;
Y y1, y2;
Only some of the following expressions will compile with a member function.
x1 == x2; // Compiles with both implementations
x1 == y1; // Compiles with both implementations
y1 == x1; // ERROR! Member function can't convert this to type X
y1 == y2; // ERROR! Member function can't convert this to type X
The solution, to get the best of both worlds, is to implement this as a friend:
class X
{
int value;
public:
friend bool operator==( X& left, X& right )
{
return left.value == right.value;
};
};
To sum up to the answer by Codebender:
Member operators are not symmetric. The compiler cannot perform the same number of operations with the left and right hand side operators.
struct Example
{
Example( int value = 0 ) : value( value ) {}
int value;
Example operator+( Example const & rhs ); // option 1
};
Example operator+( Example const & lhs, Example const & rhs ); // option 2
int main()
{
Example a( 10 );
Example b = 10 + a;
}
In the code above will fail to compile if the operator is a member function while it will work as expected if the operator is a free function.
In general a common pattern is implementing the operators that must be member functions as members and the rest as free functions that delegate on the member operators:
class X
{
public:
X& operator+=( X const & rhs );
};
X operator+( X lhs, X const & rhs )
{
lhs += rhs; // lhs was passed by value so it is a copy
return lhs;
}
There is at least one difference. A member operator is subject to access modifiers and can be public, protected or private. A global member variable is not subject to access modifier restrictions.
This is particularly helpful when you want to disable certain operators like assignment
class Foo {
...
private:
Foo& operator=(const Foo&);
};
You could achieve the same effect by having a declared only global operator. But it would result in a link error vs. a compile error (nipick: yes it would result in a link error within Foo)
Here's a real example where the difference isn't obvious:
class Base
{
public:
bool operator==( const Base& other ) const
{
return true;
}
};
class Derived : public Base
{
public:
bool operator==( const Derived& other ) const
{
return true;
}
};
Base() == Derived(); // works
Derived() == Base(); // error
This is because the first form uses equality operator from base class, which can convert its right hand side to Base. But the derived class equality operator can't do the opposite, hence the error.
If the operator for the base class was declared as a global function instead, both examples would work (not having an equality operator in derived class would also fix the issue, but sometimes it is needed).
I had a question on operator overloading. Below is my the example code. If you can read through it and my question is below.
//class definition
class example
{
private:
int a ; // Defined in FeetInches.cpp
public:
void seta(int f)
{
a = f;
}
example operator + (const example &); // Overloaded +
int geta()
{
return a;
}
};
example example::operator + (const example &right)
{
example temp;
temp.a = a + right.a;
return temp;
}
//main
#include "header" //this is the class definition above
#include <iostream>
using namespace std;
int main()
{
example r;
r.seta(1);
example s;
s.seta(1);
example t;
t = r + s;
t = r + 1; //if included it won't compile
t = 1 + r; //if included it won't compile
int x = t.geta();
cout << x;
cin.get();
return 0;
}
I understand that when you attempt to add to objects together using operator overloading they should be the same.
Here is the question:
I recently saw when the object was on one side of the operator it compiled but when it was on the other it didn't. Such as:
t = r + 1; it compiled.
t = 1 + r; it didn't.
(Also I know in my example it doesn't work either way but was just easier to frame question with code.)
How does operator overloading compile when the object is on one side of the operator but not compile when it is on the other.
Thanks
If you rewrite the offending statement verbosely, it looks like:
t.operator=(1.operator+(r));
Which doesn't make a lot of sense and confuses the compiler.
Being confused, it can either convert the number 1 to an instance of example or convert the variable r to an integer. Unfortunately, you don't supply enough information in your class to do either.
If you provide a constructor for your class that takes an integer, things may come out less confusing:
class example
{
public:
example(int new_value) : a(new_value)
{ ; }
};
Now you have provided the compiler with a method convert integers into examples.
Another alternative is to provide a casting or conversion operator:
class example
{
public:
int operator int (const example& e)
{
return e.a;
}
};
There are other alternatives, such as creating an addition method that takes an integer.
Edit 1:
If you are designing a units class beware of constants. You have no idea what unit the constant is in, such as feet or inches. The compiler won't be able to help, since numerical constants don't have units associated with them.
It's really simple, when you overloading operator you can do it in two ways:
First is like you do it. You inserted overloaded operator for your class in it. This way the left argument must be the object of this class. Why? Because when you call this operator you can do it as call of every function in your class. You can't do it other way, because you can't call this function by other type.
Second, you made overloaded operator as friend to your class.
In your class you put this line:
friend example& operator + (const example &left, const example &right);
And after your class definition you put this:
example& operator + (const example &left, const example &right){...}
So if you want to add integer or other types you must just modify your operator to add it like this:
example& operator + (const example &left, const int right){...}
or this:
example& operator + (const int left, const example &right){...}
Choose one or both if you want to add int from left or rigth side of the opearator.
t = r + 1; means t = r.operator+(1); if r defines a matching operator+() method, otherwise it means t = operator+(r, 1); instead. It does not compile because you did not define any + operator that takes an example on the left and an int on the right, eg:
// as a class method:
class example
{
...
public:
...
example operator + (int right) const;
};
example example::operator + (int right) const
{
example temp;
temp.seta(a + right);
return temp;
}
Or:
// as a global operator:
class example
{
...
public:
...
friend example operator + (const example& left, int right);
};
example operator + (const example& left, int right)
{
example temp;
temp.seta(left.a + right);
return temp;
}
If you had defined a contructor that takes an int as input, the compiler could have created a temp example when you pass an int value to the example::operator+(const example&) method, eg:
class example
{
...
public:
example (int f);
...
example operator + (const example& right);
};
example::example::(int f)
: a(f)
{
}
example example::operator + (const example& right)
{
example temp;
temp.a = a + right.a;
return temp;
}
Likewise, t = 1 + r; means t = operator+(1, r); (since 1 is not a class type). It does not compile because you did not define a global + operator that takes an int on the left and an example on the right:
class example
{
...
public:
...
friend example operator + (int left, const example& right);
};
example operator + (int left, const example& right)
{
example temp;
temp.a = left + right.a;
return temp;
}
I am trying to find a simple example program that overloads the following operators of a mathematic vector.
Constructor // create
= (equals) // assign
+; -; +=; -= // add sub
*; /; *=; /= // multi divide
++; -- // plus minus
== // compare
>; >=
<; <=
[] // access a value
Cant seem to find any good simple tutorials. I emphasize the simple because I am only learning this stuff now. If someone could link me or even better program a simple overload for just one of the operators as an example would be incredible!
There are a few things to know when you write operators, which are not as often used with other functions.
The assign operators, for example, will return *this because you change the value of the vector:
class v {
public:
double x_, y_;
v& operator += (const v& rhs)
{
_x += rhs._x;
_y += rhs._y;
return *this;
}
};
Another interesting one, the pre ++ and post ++ are different only because of an unused parameter:
class v {
public:
double x_, y_;
v& operator ++ (); // ++v
v& operator ++ (int); // v++
};
The "equal" (assignment) is another one that is tricky when you use pointers. For a vector, it generally won't be a problem, but if you define a vector V and assign it to itself, you have to be careful:
class v {
public:
double x_, y_;
v& operator = (const v& rhs)
{
if(this != &rhs)
{
x_ = rhs.x_;
y_ = rhs.y_;
}
return *this;
}
};
In your case, the if() will most certainly not be useful, but think about doing something like this:
delete p_;
p_ = new foo;
p_->x_ = rhs.p_->x_;
If &rhs == this, then the delete p_ deleted the rhs pointer! That means accessing it on the 3rd line is a bug.
The rest should be easy enough to work with. The compare operators return bool and are const:
class v {
public:
double x_, y_;
bool operator == (const v& rhs) const
{
return x_ == rhs.x_ && y_ == rhs.y_;
}
};
Although, since C++20, you are expected to only declare the three way comparison operator <=> which allows the compiler to implement all the other comparison operators for you. This one returns a negative number (smaller: a < b), 0 (equal: a == b), or a positive number (larger: a > b).
I'm not sure what makes a vector bigger or smaller, I used the length from (0, 0) in this example:
class v {
public:
double x_, y_;
int operator <=> (const v& rhs) const
{
if(x_ == rhs.x_ && y_ == rhs.y_)
{
return 0;
}
return length() > rhs.length() ? 1 : -1;
}
};
Except for the [] operator. There are two versions of that one:
class v {
public:
// I would imagine you'd use an array but as a simple example...
double x_, y_;
double operator [] (int idx) const
{
return idx == 0 ? x_ : y_;
}
v_ref operator [] (int idx)
{
v_ref v(this, idx);
return v;
}
};
As you can see, the non-constant version of the [] operator returns a reference. This is necessary so you can write something like:
r[3] = 7.3;
r[3] returns that reference, then the assignment of the reference is called with 7.3 as the parameter. (Note that we should probably throw an error if you use 3 as the index when you only have 2 values: 0 and 1--this is not shown here)
class v_ref
{
public:
v *p_;
int i_;
v_ref(v *p, int i)
: p_(p), i_(i)
{
}
operator = (double q)
{
// again, I suppose you'd use an array instead!
if(i_ == 0)
{
p_->x_ = q;
}
else
{
p_->y_ = q;
}
}
};
Assuming you want some security, the vector pointer could make use of a reference counter so you know whether a main vector object gets deleted before all of its reference objects...
Another note: I would imagine that your constructor will allocate an array of double (or use an std::vector<double> type...) If you use new, remember to delete in the destructor and that's when the if() in the assignment operator is very important.
Is there a difference between defining a global operator that takes two references for a class and defining a member operator that takes only the right operand?
Global:
class X
{
public:
int value;
};
bool operator==(X& left, X& right)
{
return left.value == right.value;
};
Member:
class X
{
int value;
bool operator==( X& right)
{
return value == right.value;
};
}
One reason to use non-member operators (typically declared as friends) is because the left-hand side is the one that does the operation. Obj::operator+ is fine for:
obj + 2
but for:
2 + obj
it won't work. For this, you need something like:
class Obj
{
friend Obj operator+(const Obj& lhs, int i);
friend Obj operator+(int i, const Obj& rhs);
};
Obj operator+(const Obj& lhs, int i) { ... }
Obj operator+(int i, const Obj& rhs) { ... }
Your smartest option is to make it a friend function.
As JaredPar mentions, the global implementation cannot access protected and private class members, but there's a problem with the member function too.
C++ will allow implicit conversions of function parameters, but not an implicit conversion of this.
If types exist that can be converted to your X class:
class Y
{
public:
operator X(); // Y objects may be converted to X
};
X x1, x2;
Y y1, y2;
Only some of the following expressions will compile with a member function.
x1 == x2; // Compiles with both implementations
x1 == y1; // Compiles with both implementations
y1 == x1; // ERROR! Member function can't convert this to type X
y1 == y2; // ERROR! Member function can't convert this to type X
The solution, to get the best of both worlds, is to implement this as a friend:
class X
{
int value;
public:
friend bool operator==( X& left, X& right )
{
return left.value == right.value;
};
};
To sum up to the answer by Codebender:
Member operators are not symmetric. The compiler cannot perform the same number of operations with the left and right hand side operators.
struct Example
{
Example( int value = 0 ) : value( value ) {}
int value;
Example operator+( Example const & rhs ); // option 1
};
Example operator+( Example const & lhs, Example const & rhs ); // option 2
int main()
{
Example a( 10 );
Example b = 10 + a;
}
In the code above will fail to compile if the operator is a member function while it will work as expected if the operator is a free function.
In general a common pattern is implementing the operators that must be member functions as members and the rest as free functions that delegate on the member operators:
class X
{
public:
X& operator+=( X const & rhs );
};
X operator+( X lhs, X const & rhs )
{
lhs += rhs; // lhs was passed by value so it is a copy
return lhs;
}
There is at least one difference. A member operator is subject to access modifiers and can be public, protected or private. A global member variable is not subject to access modifier restrictions.
This is particularly helpful when you want to disable certain operators like assignment
class Foo {
...
private:
Foo& operator=(const Foo&);
};
You could achieve the same effect by having a declared only global operator. But it would result in a link error vs. a compile error (nipick: yes it would result in a link error within Foo)
Here's a real example where the difference isn't obvious:
class Base
{
public:
bool operator==( const Base& other ) const
{
return true;
}
};
class Derived : public Base
{
public:
bool operator==( const Derived& other ) const
{
return true;
}
};
Base() == Derived(); // works
Derived() == Base(); // error
This is because the first form uses equality operator from base class, which can convert its right hand side to Base. But the derived class equality operator can't do the opposite, hence the error.
If the operator for the base class was declared as a global function instead, both examples would work (not having an equality operator in derived class would also fix the issue, but sometimes it is needed).