C/C++ study notes eight

1. Constructor automatic conversion

        Judging from the sentence CUser User = 2;, it stands to reason that it cannot be compiled, but the CUser(int nData) constructor is actually called here.

However, when developing, sometimes it is necessary to disable the automatic conversion function of the constructor (implemented by adding the explicit keyword in front of the constructor), because it is sometimes misleading.

#include "stdafx.h"
#include "iostream.h"
#include "string.h"

class CUser										//定义一个类
{
public:											//设置成员访问级别
	char m_Username[128];							//定义数据成员
	char m_Password[128];							//定义数据成员
	int	 m_nLevel;								//操作员级别
public:	
	CUser()										//默认构造函数
	{
		m_nLevel = 1;
		strcpy(m_Username, "MR");					//为数据成员赋值
		strcpy(m_Password, "KJ");					//为数据成员赋值
		cout << "构造函数被调用!" << endl;				//输出信息
	}
	CUser(int nData)								//构造函数
	{
		m_nLevel = nData;
		cout << "复制对象!" << endl;					//输出信息
	}
};
int main(int argc, char* argv[])
{
	CUser User = 2;								//为User对象赋值
	cout << "m_nLevel = " << User.m_nLevel << endl;		//输出信息
	return 0;
}

2. Operator overloading

        If the + operator is overloaded in the class CUser, the addition of two objects of the same type can be implemented.

#include "stdafx.h"
#include "iostream.h"
#include "string.h"

class CUser										//定义一个类
{
public:											//设置成员访问级别
	char m_Username[128];							//定义数据成员
	char m_Password[128];							//定义数据成员
	int	 m_nLevel;								//操作员级别
public:	
	CUser()										//默认构造函数
	{
		m_nLevel = 1;
		strcpy(m_Username, "MR");					//为数据成员赋值
		strcpy(m_Password, "KJ");					//为数据成员赋值
		cout << "构造函数被调用!" << endl;				//输出信息
	}
	CUser operator + (CUser & refUser)				//重载+运算符
	{
		CUser User;
		User.m_nLevel = m_nLevel + refUser.m_nLevel;
		return User;
	}
};

int main(int argc, char* argv[])
{
	CUser User, comUser;
	CUser defUser = User + comUser;					//进行加法运算
	cout << "m_nLevel = " << defUser.m_nLevel << endl;
	return 0;
}

        If the user wants to implement the addition of a CUser object to an integer, he can do so by modifying the parameters of the overloaded operator.

	CUser operator + (int nData)				//重载+运算符
	{
		CUser User;
		User.m_nLevel = m_nLevel + nData;
		return User;
	}

         In this way, the addition of a CUser object and an integer can be achieved.

    CUser User;
    CUser defUser = User + 10;

        Pre-operation

	CUser operator ++ ()				//重载++运算符
	{
		++m_nLevel;
	}

3. Conversion operator

#include "stdafx.h"
#include "iostream.h"
#include "string.h"


class CUser										//定义一个类
{
public:											//设置成员访问级别
	char m_Username[128];							//定义数据成员
	char m_Password[128];							//定义数据成员
	int	 m_nLevel;								//操作员级别
public:	
	CUser()										//默认构造函数
	{
		m_nLevel = 1;
		strcpy(m_Username, "MR");					//为数据成员赋值
		strcpy(m_Password, "KJ");					//为数据成员赋值
		cout << "构造函数被调用!" << endl;				//输出信息
	}
	operator int ()									//转换运算符
	{
		return m_nLevel;
	}
};

int main(int argc, char* argv[])
{
	CUser User;
	int nData = User;								//将CUser对象赋值给一个整型变量
	cout << "m_nLevel = " << nData << endl;
	return 0;
}

4. Summary

        When the user overloads the operator in the program, the following rules need to be followed:

        (1) Not all C++ operators can be overloaded.

        Overloading is possible for most operators in C++. But "::", "?", ":", "." operators cannot be overloaded.

        (2) Some limitations of operator overloading.

        Cannot construct new operators.

        The number of operands of the original operator cannot be changed.

        The precedence of the original operator cannot be changed.

        The associativity of the original operator cannot be changed.

        The syntax structure of the original operator cannot be changed.

        (3) Basic principles of operator overloading.

        A unary operand can be a member function that takes no parameters, or a non-member function that takes one parameter.

        A binary operand can be a member function that takes one parameter, or a non-member function that takes two parameters.

        "=", "[]", "->", "()", operators can only be defined as member functions.

        The return value of the "->" operator can only be a pointer type or can use the "->" operator type object.

        When overloading the "++" and "--" operators, take an int type parameter, which means post-operation, and no parameter means pre-operation.

Guess you like

Origin blog.csdn.net/bashendixie5/article/details/124430145