C++ pointer, this pointer, static member

C++ pointer, this pointer, static member

I. C++ this Pointer

This pointer points to the invoked object itself, and in the member function, there will be a default parameter this. If you've learned Python, you might know that every member function in Python has a parameter self, and this is something like that. In fact, specifically, this pointer is also a pointer, it also stores an address. When we call a member function with an object, we pass the address of the object to the function, which is the this parameter in the function. So the address of the calling object is stored in this pointer.
Let's look at a simple example:

#include <iostream>
using namespace std;

class Line
{
	public:
		int getL();
		void setL(int l);
		void printL();
		Line();
		~Line();
	private:
		int len;
};

Line::Line()
{
	cout << "create object" << endl;
}

Line::~Line()
{
	cout << "Release memory" << endl;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}

void Line::printL()
{
	int temp;
	temp = this->getL();
	cout << "Len is:" << temp << endl;
}



int main()
{
   Line line,line2;
	line.setL(10);
	line2.setL();
	line.printL();
	line2.printL();
   return 0;
}

Many problems can be found in programming. When using pointers to get members of structures or classes, we can't use the'. 'operator. We need to use - > to access them without error. Secondly, for how a member function uses the default values of parameters, it is not necessary to give default function parameters when defining them in a class. Otherwise, they will make mistakes.
Operation results:

create object
 create object
Len is:10
Len is:3
 Release memory
 Release memory

I just saw that the printL () function uses this pointer to call member functions and member variables. There is another way to do the same thing, which is the friend function. Friend functions do not belong to classes, but they have access to all members of the class, including private members and protected members. Unlike member functions, it does not have this pointer, and it does not belong to a class.

#include <iostream>
using namespace std;

class Line
{
	public:
		friend void friend_test(Line line);
		int getL();
		void setL(int l);
		void printL();		
		Line();
		~Line();
	private:
		int len;
};

Line::Line()
{
	cout << "create object" << endl;
}

Line::~Line()
{
	cout << "Release memory" << endl;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}

void Line::printL()
{
	int temp;
	temp = this->getL();
	cout << "Len is:" << temp << endl;
}

void friend_test(Line line)
{
	cout << "Len is :"<< line.len << endl;
}

int main()
{
   Line line,line2;
	line.setL(10);
	line2.setL();
	line.printL();
	line2.printL();
	friend_test(line);
	friend_test(line2);
   return 0;
}

Operation results:

create object
 create object
Len is:10
Len is:3
Len is :10
 Release memory
Len is :3
 Release memory
 Release memory
 Release memory

As you can see, private variables can be accessed directly in friend functions.

II. Pointer of Class

A pointer to a class stores the address of an object in memory as a pointer to a common variable. Just like the this pointer above.
Look at a small example:

#include <iostream>
using namespace std;

class Line
{
	public:
		int getL();
		void setL(int l);
		Line();
		~Line();
	private:
		int len;
};

Line::Line()
{
	cout << "create object" << endl;
}

Line::~Line()
{
	cout << "Release memory" << endl;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}



int main()
{
    Line line,line2;
	Line *p,*q;
	p = &line;
	q = &line2;
	p->setL(10);
	q->setL();
	cout << "line Len is: " << p->getL() << endl;
	cout << "line2 Len is:" << q->getL() << endl;
	
   return 0;
}

Operation results:

create object
 create object
line Len is: 10
line2 Len is:3
 Release memory
 Release memory

It is important to note that a pointer to a class needs to use the - > operator when accessing a member of the class.

III. Static Membership of Classes

A static member of a class means that all objects of that class share this variable and will not be copied with the creation of the object. The popular point is that the address of the variable modified with static is locked and cannot be changed.
The use of static members, which need to be initialized when static members are defined. If there is no initialization, then when the class creates the first object, it automatically initializes all static member variables with a value of 0, and initializes static variables defined in the class when they cannot be defined in the class. You can initialize with: type class name: variable name = 0.

#include <iostream>
using namespace std;

class Line
{
	public:
		static int count;
		int getL();
		void setL(int l);
		Line();
		~Line();
	private:
		int len;
};

int Line::count = 0;

Line::Line()
{
	cout << "create object" << endl;
	count++;
}

Line::~Line()
{
	cout << "Release memory" << endl;
	count--;
}

void Line::setL(int l = 3)
{
	this->len = l;
}

int Line::getL()
{
	return this->len;
}



int main()
{
    Line line;
	cout << "The objects that have been created are:"<< line.count << " individual" <<endl;
	Line lines[3];
	cout << "The objects that have been created are:"<< line.count << " individual" <<endl;	
   	return 0;
}

Operation results:

create object
 The objects that have been created are: 1
 create object
 create object
 create object
 There are four objects that will be created
 Release memory
 Release memory
 Release memory
 Release memory

Keywords: Python Programming

Added by chu70077 on Thu, 08 Aug 2019 10:35:11 +0300