[C ++ classes and objects]-the relationship between this pointer and member methods, pointers to class members

First, the relationship between this pointer and member methods

Take the commodity class we wrote in the previous article, we all know that all the member methods in it are ordinary member methods.
The commodity code is implemented as follows:

class CDate
{
public:
	CDate(int y, int m, int d)
	{
		_year = y;
		_month = m;
		_day = d;
	}
	void show()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
class CGoods
{
public:
	CGoods(const char* n, int a, double p,int y,int m,int d):_data(y,m,d)
	{
		strcpy(_name, n);
		_amount = a;
		_price = p;
	}
	void show()
	{
		cout << "name: " << _name << endl;
		cout << "amount:" << _amount << endl;
		cout << "price:" << _price << endl;
		_data.show();
	}
private:
	char _name[20];
	int _amount;
	double _price;
	CDate _data;
};

1. Ordinary member methods

Ordinary member methods must be called by the object, because after compilation, the address of the object will be passed to the function as an actual parameter. During the compilation process, these member methods will add an additional this parameter variable to receive his calling object.
He has the following characteristics:

  • Scope belonging to class
  • You need to rely on an object when calling this method (often objects cannot be called)
  • Private member variables that can access the object arbitrarily do not consider protected, only consider public and private

2. Static member methods

If we want to add a requirement to the commodity category mentioned earlier-count the total number of all commodities

Improvement one: add a _count ++ in the constructor; record the number of all new objects generated

CGoods(const char* n, int a, double p, int y, int m, int d)
		:_data(y, m, d)
		{
			strcpy(_name, n);
			_amount = a;
			_price = p;
			_count++;
		}

Improvement 2: Add a static member method to print the information shared by all commodities

static void showCGoodsCount()
	{
		cout << "所有商品的种类数量是: " << _count << endl;
	}

Improvement three: Define a static member variable in private.

private:
	char _name[20];
	int _amount;
	double _price;
	CDate _data;
	static int _count;

This is just a statement, to be defined and initialized outside the class. It does not belong to the object, but belongs to the class level. Does not occupy object memory, in the .data section

Improvement 3: Initialize outside the class

int CGoods::_count = 0;

Static member method characteristics: this parameter will not be generated

  • Scope belonging to class

  • Call method with class name scope

  • Can access the private members of the object arbitrarily, limited to members that do not depend on the object (only other static members can be called)

    The difference between ordinary member methods and static member methods: the
    former has a CGoods * this, the latter does not need this pointer does not need to receive the address of an object call

3. Regular member methods

Add a requirement on the basis of the above commodity class. To declare a commodity object in the main function, the commodity can only be viewed

int main()
{
const CGoods good5("非卖品商品5",100, 35.0, 2019, 5, 12);
	good5.show();
	return 0;
}

Note that the common method show () called here will be wrong, because the common object calls the common method CGoods :: show (& good5) const CGoods *-> ​​CGoods * this error, the method called by the common object must be the usual method

Improvement one: define a common method of a show

void show() const//const CGoods *this
	{
		cout << "name: " << _name << endl;
		cout << "amount:" << _amount << endl;
		cout << "price:" << _price << endl;
		_data.show();
	}

The show called by time should also be the usual method

void show() const
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}

Constant member methods, and ordinary member methods overload ordinary objects and constant objects can be called. As long as the member methods of the read-only operation, all const constant member methods are implemented

Constant member method features: generate const CGoods * this

  • Scope belonging to class
  • The call depends on an object, either ordinary objects or regular objects
  • Can access the private members of the object arbitrarily, but can only read, not write

Two, pointers to class members

We have such a class:

class Test
{
public:
	void func() { cout << "call Test :: func" << endl; }
	static void static_func() { cout << "Test::static_func" << endl; }
	int ma;
	static int mb;
};
int Test::mb;

How should we implement it when we want to define a pointer to a member variable or method?
note! The pointer must be preceded by a class scope
(1) pointer to ordinary member variables

Test t1;
	Test* t2 = new Test();
int Test::* p = &Test::ma;
	t1.*p = 20;
	cout << t1.*p << endl;

	t2->*p = 30;
	cout << t2->*p << endl;
	delete t2;

(2) Pointer to static member variable, not dependent on object

int* p1 = &Test::mb;
	*p1 = 40;
	cout << *p1 << endl;
	

(3) Pointer to member method (ordinary member method must depend on the object)

void (Test :: * pfunc)() = &Test::func;
	(t1.*pfunc)();
	(t2->*pfunc)();
Published 98 original articles · won praise 9 · views 3672

Guess you like

Origin blog.csdn.net/qq_43412060/article/details/105098230