c ++ operator overloading this usage const pointer usage super full!

Second, the member function overloading

Format member function prototypes:

Operator operator function type (parameter table);

Member function definition format:

函数类型 类名::operator 运算符(参数表)

{

	函数体;

}

About this pointer:

this is an implicit inline pointer to the current object member function calls.

this pointer is passed in the form of hidden parameters to the non-static member function.

In addition to this pointer returns the current object, but also often in a non-static function members.

Third, the non-member function overloading

Friend function prototype format:

Operator Type operator friend function (parameter table);

Friend function defined in the format:

Type Function Operator :: operator class name (parameter list)

{

Function thereof;

}

Behind the non-static const member functions plus

(Later added to the non-member function or static member gives compiler error),

The member function implicitly passed this pointer is const pointer,

In this decision the member function, any modification operations in which it is a member of the class are not allowed (because of this implicit const pointer references);

The only exception is modified for mutable members.

Plus the const member functions can be called non-const objects and const object, but without the const member functions can only be called a non-const object.

Case

int f() const
{
	return a;//同return this->a;
}

  #include <iostream>
using namespace std;
class Complex
{
	public:
		Complex(double r=0, double i=0):real(r), imag(i){	}
		Complex operator+( Complex& c )const;//重载双目运算符'+'
		Complex operator-=( Complex& c ); //重载双目运算符'-='
		friend Complex operator-(const Complex& c1,const Complex& c2 );//重载双目运算符'-'
		void Display() const;
	private:
		double real;
		double imag;
};

void Complex::Display() const
{
	cout << "(" << real << ", " << imag << ")" << endl;
}
Complex Complex::operator+( Complex &c) const
{
    Complex tmp;
    tmp.real=this->real+c.real;
    tmp.imag=this->imag+c.imag;
    return tmp;
}
Complex Complex::operator-=(Complex &c)
{
	
    this->real-=c.real;
    this->imag-=c.imag;
    return *this;

}

Complex operator-(const Complex &c1, const Complex &c2)
{
	Complex tmp;
	tmp.real=c1.real-c2.real;
	tmp.imag=c1.imag-c2.imag;
	return tmp;
}

int main()
{
	double r, m;
	cin >> r >> m;
	Complex c1(r, m);
	cin >> r >> m;
	Complex c2(r, m);
	Complex c3 = c1+c2;
	c3.Display();
	c3 = c1-c2;
	c3.Display();
	c3 -= c1;
	c3.Display();
	 return 0;
}

const usage summary

  1. Const action is to rewrite the object is not

    - = This symbol will c3 rewritten so without const

  2. const can be placed outside of the function can be placed within brackets brackets function
    but outside the class must match the class

As a member function

/ Const / Time operator + (const Time T &) const;
first return value const is a modified, but doing so has little effect, generally do not write
the second parameter is a modified const (right operand additions), showing that the function (addition operation) does not modify the parameter content. If the error will not add const, const operand is passed, it is generally write the
third const indicates that the function itself does not modify the object itself (ie * this, but also the addition of the left operand do) content . If not cons, the objects will be given when you call const addition, it is generally write on

As a friend function

Friend / const / Time operator + (const & Time t1, const Time & T2);
a first first const const member functions the same way with the
parameters corresponding to t1 * this (left operand adder) when member function, so the const and the end of the const member functions the same way
the parameters corresponding to the parameters T t2 (right operand adder) when member function, so the second const const member functions in the same way and

Friend function at the end can not write const (be sure to remember !! I have just been given the time to write !!!)

Guess you like

Origin blog.csdn.net/weixin_44769592/article/details/92377930