Copy constructor, this pointer, assignment operator overload, complete class

Posted by srhino on Sat, 25 Sep 2021 19:04:03 +0200

Catalog

copy constructor

this pointer

Because this pointer points to its own object, we return this pointer to return our own object

Assignment Operator Overload (Assignment Constructor, have I forgotten this statement, I've always called it that way)

Complete class (parameterized, parameterized, copy, assignment, destructive)

copy constructor

Copy Constructor (can be understood as one of the constructors)

If no default copy constructor is provided, the system will not.

The default copy constructor provided by the system is a shallow copy.

Shallow copy, one address assigned to another address, two addresses pointing to the same space. When released, the same space will be released twice, crashing the program.

Deep copy, open up the space that the source address points to for the address to be assigned, and copy the two spaces one by one.

Declared the same way as a constructor but fixed parameters are references to const classes

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>

using namespace std;

class Student
{
public:

	//Constructor
	Student(const char* name)
	{
		m_name = new char[strlen(name) + 1];
		strcpy(m_name, name);
	}

	//copy constructor
	Student(const Student& s)
	{
		//Shallow copy only assigns addresses that point to the same block space
		m_name = s.m_name;	//VS is only a program crash, it is not saved in GCC environment will generally report double free

		//deep copy
		//m_name = new char[strlen(s.m_name) + 1];
		//strcpy(m_name, s.m_name);
	}

	~Student()
	{
		delete[]m_name;
	}

	void studentShow()const
	{
		cout << m_name << endl;
	}

private:
	char* m_name;
};

int main()
{
	//Call Constructor
	Student s = "hello world";

	//Call copy construction
	Student s2(s);
	
	s.studentShow();
	s2.studentShow();

	return 0;
}

Copy constructor. Actually it's also a constructor

It is worth mentioning that we have recently seen several discussions on the issue of right-value references, and interested partners can look for information. Actually, this is not something very new. I don't know why it has suddenly caught fire recently.

Assignment constructors have a moving constructor in addition to the constructor.

Look with the right value reference. It is recommended that you search for "moving constructors and right value references".

this pointer

When an object is created, a pointer to the current object, named this, is generated by default.

This pointer is used inside a class, and the const qualified by the function () const is the this pointer

#include <iostream>

using namespace std;

class A
{
public:

	A()
	{
		this->m_a = 10;
		this->m_b = 20;
		this->m_c = 30;
	}

	void aShow()
	{
		//this pointer
		cout << this << endl;
		cout << this->m_a << " " << this->m_b << " " << this->m_c << endl;
	}

private:
	int m_a;
	int m_b;
	int m_c;
};

int main()
{
	A a;
	//Object a has the same address as the aShow output
	cout << &a << endl;
	a.aShow();

	return 0;
}

 

Because this pointer points to its own object, we return this pointer to return our own object

#include <iostream>

using namespace std;

class A
{
public:

	A()
	{
		this->m_a = 10;
		this->m_b = 20;
		this->m_c = 30;
	}

	void aShow()
	{
		//this pointer
		cout << this << endl;
		cout << this->m_a << " " << this->m_b << " " << this->m_c << endl;
	}

	//Double a with a single call
	A* m_aAdd()
	{
		this->m_a *= 2;
		return this;
	}

	//Of course we don't need to return pointers for references in C++.
	A& m_bAdd()
	{
		this->m_b *= 2;
		return *this;	//Unreferencing this pointer takes the object itself
	}

private:
	int m_a;
	int m_b;
	int m_c;
};

int main()
{
	A a;
	//Object a has the same address as the aShow output
	cout << &a << endl;
	a.aShow();
	cout << endl;

	//Since it returns a pointer to its own object, it can always be called with a member operator
	a.m_aAdd()->m_aAdd()->m_aAdd()->aShow();
	cout << endl;

	//Since it returns its own object, it can always be called with a member operator
	a.m_bAdd().m_bAdd().m_bAdd().aShow();

	return 0;
}

Assignment Operator Overload (Assignment Constructor, have I forgotten this statement, I've always called it that way)

Called when one existing object assigns values to another existing object

If not, a shallow copy of the assignment constructor is automatically generated

Statement is fixed

Return class reference operator=(const class reference)

Assignment constructors automatically generated by the system not only release a piece of space twice, but also cause memory leaks. Since it is an assignment behavior between two existing objects, not releasing memory on the assigned side directly changes the pointer to a new space, causing memory leaks.

Complete class (parameterized, parameterized, copy, assignment, destructive)

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

using namespace std;

class Student
{
public:

	//non-parameter constructor
	Student()
	{
		this->m_name = new char[1];
		*this->m_name = 0;
	}

	//Parametric constructor
	Student(const char* name)
	{
		m_name = new char[strlen(name) + 1];
		strcpy(m_name, name);
	}

	//copy constructor
	Student(const Student& s)
	{
		m_name = new char[strlen(s.m_name) + 1];
		strcpy(m_name, s.m_name);
	}

	//Assignment operator overload (I've always called it an assignment constructor anyway)
	Student& operator=(const Student& s)
	{
		//Because the original space needs to be freed, if you don't make this judgment, the freed space will be assigned (value is uncertain)
		if (m_name == s.m_name)
		{
			return *this;
		}

		if (this->m_name != nullptr)
		{
			delete[]this->m_name;
			this->m_name = nullptr;
		}

		this->m_name = new char[strlen(s.m_name) + 1];
		strcpy(this->m_name, s.m_name);

		return *this;
	}

	//Destructor
	~Student()
	{
		if (this->m_name != nullptr)
		{
			delete[]this->m_name;
			this->m_name = nullptr;
		}
	}

	void studentShow()
	{
		cout << this->m_name << endl;
	}

private:
	char* m_name;
};

int main()
{
	//Call parametric construction
	Student s = "hello world";

	//Call parameterless constructor
	Student s2;

	//Call copy constructor
	Student s3(s2);

	//Call assignment constructor
	s2 = s3 = s;

	s.studentShow();
	s2.studentShow();
	s3.studentShow();

	return 0;
}

Topics: C++ Javascript html5 html