2023.5.10 (C++ notes)

Table of contents

inner class

Inner classes are natural friends of outer classes;

Anonymous objects (destroyed upon use)

Compiler optimization of construction

 C/C++ memory management

new and delete (used in pairs)

The difference between new and malloc


inner class

There are two types of inner classes: public inner classes and private inner classes.

But it is restricted by access qualification

Inner classes do not take up space when declared in the class, only the definition takes up space.

After locating a category B, it becomes 12;

Inner classes are natural friends of outer classes;

The inner class can access the data of the outer class, but the outer class cannot access the information of the inner class.

Anonymous objects (destroyed upon use)

Class name + (parameter)

Anonymous objects have permanence

const A& ra = A(2);//const reference extends the life cycle of the anonymous object, and the life cycle is in the local domain of the current function,

//匿名对象
class A
{
public:
	//构造函数
	A(int a=1)
		:_a(a)
	{
		cout << "A(int a)" << endl;
	}

	~A()
	{
		cout << "~A()" << endl;
	}

private:
	int _a=1;
};

class Solution
{
public:
	Solution()
	{
		
	}

	~Solution()
	{
		cout << "~Solution()" << endl;
	}

	int Sum_Solution(int n)
	{
		cout << "Sum_Solution" << endl;

		return n;
	}
};

	int main()
	{
		A a(2);//有名对象--生命周期在当前函数局部域

		//匿名对象即用即销毁
		A(2);//匿名对象--生命周期在当前行
		A();//可以理解成后面就没人用了,就直接销毁了


		// A& ra = A(1);//匿名对象具有常性
		const A& ra = A(2);//const引用延长匿名对象的生命周期,生命周期在当前函数局部域,
		//可以理解为因为后面ra还会用这个匿名对象,所以没有销毁



		//创建对象,调用函数
		 Solution b;
	
		//有名对象不能这样写  Solution A();  因为编译器不知道这是对象,还是函数声明
		 b.Sum_Solution(10);
		

		//匿名对象调用函数,还是会先走构造函数,再进入调用的函数,必须加一个括号,
		//有默认构造函数不要传参,没有的话正常传参
		Solution().Sum_Solution(10);
		//Solution::Sum_Solution(10);错误调用,没有this指针可以传递


		return 0;
	}

Types with const and without const are different

Can constitute function overloading

Compiler optimization of construction

Continuous construction + copy construction occurs in one expression on the same line, and the optimization is combined into one

optimization

And aa=1;

Not optimized

A aa1;

Func(aa1);

When this happens, try to write it in one line

 

 C/C++ memory management

The addresses decrease from top to bottom.

From the language perspective, it is called the static area, and from the system perspective, it is called the data segment.

Constant area/code segment

The essence of establishing a stack frame is to store local data and make recursive calls.

Because programs have different needs, they need to allocate memory and divide areas.

c c c a a 

a a a d a b

strlen encounters '\0' cutoff, not counting '\0'

32-bit pointer size is 4, 64-bit pointer size is 8

* When the array name is used, the array name represents the address of the first element

sizeof(array name), the array name represents the entire array

const will not affect the data storage area. It cannot be considered that adding const is in the constant area.

You can see that the addresses of a and b are very close to each other, so we can judge that b is also on the stack.

 

When the expansion space is not enough, realloc will automatically destroy the original space and then transfer the data to a new location;

 

new and delete (used in pairs)

, cannot be mixed with free, unknown problems may occur

The difference between new and malloc

malloc opens space

 new Open space + call the object's constructor initialization

struct ListNode
{
	int _val;
	struct ListNode* _next;

	ListNode(int x)
		:_val(x)
		, _next(NULL)
	{}
};

struct ListNode* BuyListNode(int x)
{
	// 单纯开空间
	struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
	// 检查
	newnode->_next = NULL;
	newnode->_val = x;

	return newnode;
}

int main()
{
	struct ListNode* n1 = BuyListNode(1);
	struct ListNode* n2 = BuyListNode(2);
	struct ListNode* n3 = BuyListNode(3);

	// 开空间+用对象调用构造函数初始化
	ListNode* nn1 = new ListNode(1);
	ListNode* nn2 = new ListNode(2);
	ListNode* nn3 = new ListNode(3);

	return 0;
}

Guess you like

Origin blog.csdn.net/2202_75625589/article/details/130671369