Term
Are there any errors or inefficiencies in this code?
#include <iostream> using namespace std;
class Integer { int value; public: explicit Integer(const int& value) : value(value) {}
Integer operator+(Integer v) { return Integer(value + v.value); } };
int main() { Integer a(2); Integer b(3); Integer c = a + b;
return 0; }
|
|
Definition
Integer operator+(Integer v)
unnecessarily calls copy constructor of Integer
Use this instead
Integer operator+(const Integer& v)
|
|
|
Term
Now using the fixed code, will this work?
#include <iostream> using namespace std;
class Integer { int value; public: explicit Integer(const int& value) : value(value) {}
Integer operator+(const Integer& v) { return Integer(value + v.value); } };
int main() { const Integer ONE(1); Integer a(23); Integer c = ONE + a; return 0; }
|
|
Definition
Nope, the + operator doesnt promise that it wont affect the state of the instance calling it
ONE + a = ONE.operator+(a)
Integer operator+(const Integer& v) const
|
|
|
Term
Will this code run?
#include <iostream> using namespace std;
class Integer { int value; public: explicit Integer(const int& value) : value(value) {}
Integer operator+(const Integer& v) const{ return Integer(value + v.value); } };
int main() { const Integer ONE(1); Integer a(23); Integer c = ONE + a = a;
return 0; }
|
|
Definition
Yes, we need to set the return of operator+ a const so that its not reassigned like that
const Integer operator+(const Integer& v) const{ return Integer(value + v.value); }
First const to avoid re-assigning
Second const and & to avoid unnecessary copying using copy constructor
Third const to allow const instances to work |
|
|
Term
++prefix, are there any issues here?
#include <iostream> using namespace std;
class Integer { int value; public: explicit Integer() : value(0) {}
const Integer operator++(const Integer& v) const{ return Integer(++v.value); } };
int main() { Integer a; ++a;
return 0; }
|
|
Definition
This whole function is wrong
++a should increment the instance 'a' itself and give the instance itself.
Remove the parameter, we want to change this we dont need another Integer.
Remove the last const, the whole point of ++ is to change the instance state.
(Slides remove the first const, idk why)
const Integer operator++() { ++value; return *this; }
|
|
|
Term
What could not marking the const let you do?
Integer& operator++() { ++value; return *this; }
|
|
Definition
int main() { Integer a; ++++++a; cout << a; return 0; }
I think thats kinda neat |
|
|
Term
Is this correct for postfix?
Integer& operator++(int) { value++; return *this; }
|
|
Definition
Absolutely not
This is just the same as doing ++a;
a++ increments a but returns the original state of 'a'.
Makes sure to return Integer and not Integer& because temp will be deleted after it leaves the function. Return type Integer gives a copy.
Integer operator++(int) { Integer temp = *this; value++; return temp; }
|
|
|
Term
Stream overloading
Whats wrong with this definition?
ostream& operator<<(ostream& os ) { os << value; return os; }
|
|
Definition
.... we cant to cout << x;
int main() { Integer x; x << cout; return 0; }
operator functions take instances on the left side therefore we need to define a free function, however if the stream operator needs access to private attributes, you need to make it a friend
|
|
|
Term
Does this work?
friend ostream& operator<<(ostream & os, const Integer & v) { os << v.value; return os; }
int main() { Integer a; cout << ++a; return 0; }
|
|
Definition
|
|
Term
Does this work?
class Integer{ .... friend ostream& operator<<(ostream & os, const Integer & v) { os << v.value; return os; } };
int main() { Integer a; cout << ++a; return 0; }
|
|
Definition
|
|
Term
What if we make the member function a const?
friend ostream& operator<<(ostream & os, const Integer & v) const { os << v.value; return os; }
|
|
Definition
THIS ISNT A CLASS MEMBER, ITS BASICALLY A FREE FUNCTION, EVEN IF I DEFINE AND DECLARED IT IN THE CLASS. |
|
|
Term
What if I define os as a const?
friend ostream& operator<<(const ostream & os, const Integer & v) { os << v.value; return os; }
|
|
Definition
You cant do os << v.value anymore because os is const... |
|
|
Term
What if I define the return as const?
class Integer{ ...
friend const ostream& operator<<(ostream & os, const Integer & v) { os << v.value; return os; } };
int main() { Integer a; cout << ++a; return 0; }
|
|
Definition
1
works...BUT you cant chain streams anymore on the right side of a.
int main() { Integer b; cout << "Integer: " << b << endl; return 0; }
b << endl; causing error |
|
|
Term
casting operators, is this correct?
operator int() const { return value; }
int main() { Integer a(42); int c = a; cout << c << endl; return 0; }
|
|
Definition
42
YES,
REMINDER do not give it a return type at all,
int operator int(){return 1;} WRONG
operator int(){return 1;} CORRECT |
|
|
Term
Does this work?
const Integer& operator=(const Integer& I) { value = I.value; return *this; }
|
|
Definition
|
|
Term
What will be the output?
#include <iostream> using namespace std;
class Integer { int value; public: Integer() : value(0) { cout << "Default Constructor" << endl; }
Integer(const int& i): value(i) { cout << "Parameterized Constructor" << endl; };
Integer(const Integer& I): value(I.value) { cout << "Copy Constructor" << endl; };
const Integer& operator=(const Integer& I) { cout << "Assignment Operator " << I.value << endl; value = I.value; return *this; } };
int main() { Integer x; Integer y = x; Integer z = Integer(34); Integer p; p = z;
return 0; }
|
|
Definition
Default Constructor Copy Constructor Parameterized Constructor Default Constructor Assignment Operator 34 |
|
|
Term
How do you add an Integer and an int?
|
|
Definition
Integer + int
Integer operator+(const int &v1) { return Integer(v1 + value); }
|
|
|
Term
How do you do int + Integer?
|
|
Definition
friend Integer operator+(const int &v1, const Integer &v2) { return Integer(v1 + v2.value); }
You cant define int on left side for int + Integer, so you need to make a free function |
|
|