Lecture 6 - Advanced Class Operations
Lecture 6 - Advanced Class Operations
Lecture 6
Advanced Class Operations
Dr C.F. Kwong
chiew-foong.kwong@nottingham.edu.cn
Department of Electrical and Electronic Engineering
Faculty of Science and Engineering
Room: PMB 310
Topics
Introducing ”This” operator
Assignment Operator
Static Members and Functions
Overloading Operators
The this Pointer
this: predefined pointer available to a class’s
member functions
Always points to the instance (object) of the class
whose function is being called
Is passed as a hidden argument to all non-static
member functions
Can be used to access members that may be hidden
by parameters with same name
this Pointer Example
class SomeClass
{
private:
int num;
public:
void setNum(int num)
{ this->num = num; }
...
};
Introducing “this”
const int_vector& int_vector::operator=(const int_vector& a){
if(this==&a) return(*this);
......
It would be silly to waste time doing x=x if x is big - so we need to detect this somehow.
However, this means that the instance of the class being copied into must know who they are!
20000000
values 4000000
“if my address is the same as that of the argument then the argument must be me!”
public:
~int_vector(); // Destructor
// Assignment
private: To A.set_value(0,1.23);
pe
int number_of_elements; rf A.set_value(1,2.45);
or
m
int *values; int_vector B(10);
};
B=A;
}
Assignment Operator
const int_vector& int_vector::operator=(const int_vector& a){
if(this==&a) return(*this);
if(number_of_elements !=a.number_of_elements) {
// The existing array is not the same size as that of a, so destroy it and create a new one
number_of_elements =a.number_of_elements;
delete[] values;
values=new int[number_of_elements];
}
return(*this);
}
Difference with Copy Constructor
We have dealt with the first line, (copy constructor) but there is a difference in the
second line.
z already exists when we copy the function’s return value into it. We don’t want to
create a new float with z=func(x); just change the values of the existing object, z.
This is an assignment not a copy construction.
The basic syntax of the assignment operator is always the same, both
argument and return value are const references to an instance.
operator=: overwrite the data of one existing instance with a copy of the data of another
existing instance
// Here’s a little time saver, given how similar the implementations often are. Is it always “safe to do”?
class my_object() {
...
my_object(const my_object& an_object_to_be_copied) { // Copy constructor
*this= an_object_to_be_copied;
const my_object& operator=(const my_object& an_object_to_be_copied); // Operator=
};
Summary
In order that our class objects behave just like the intrinsic types such as float, 4 special
member functions are always needed; constructor, destructor, copy constructor,
assignment operator.
If we do not provide them then the compiler will make its own guess for them which is
unreliable.
All well designed classes must be explicitly given these 4 functions. The syntax of the
definitions is always the same.
class my_object() {
public:
~my_object(); // Destructor
Prototype:
void operator=(const SomeClass &rval)
parameter for
return function object on right
type name side of operator
Operator is called via object on left side
Invoking an Overloaded Operator
For example it would be nice to add complex numbers using a simple +, i.e.
complex a,b,c;
...
c=a+b;
sum.re+=re;
sum.im+=im;
Straightforward! Now we can define the full suite of +,-,*,/ in exactly the
same way