C++ Advanced Programming 01

Table of contents

1. Double colon scope operator

2. namespace namespace

3. using statement and using compilation instruction

using statement

4. C++ enhances C language

4. const link attribute

5. Const allocation of memory

6. Try to use const instead of define

7. Quote


1. Double colon scope operator

    ::Represents the scope If nothing is added in front of it, it represents the global scope

 

int atk = 1000;

void test01()
{
	int atk = 100;
	cout << "atk =" << atk << endl;


	//::代表作用域,如果前面什么都不加代表全局作用域
	cout << "全局atk = " << ::atk << endl;
}

result

atk=100
global atk=1000

2. namespace namespace

  1. Namespace purpose: resolve name conflicts
  2. Namespaces can store: variables, functions, structures, classes...
  3. Namespaces must be declared at the global scope
  4. Namespaces can nest namespaces
  5. The namespace is open, and new members can be added to the namespace at any time
  6. Namespaces can be anonymous
  7. Namespaces can be aliased

write one first

Header file for game1

#include"game1.h"

void KingGlory::goAtk()
{
	cout << "王者荣耀攻击我" << endl;
}

header file of game2.h

#include <iostream>
using namespace std;

namespace LOL
{
	void goAtk();
}

file of game1.c

#include"game1.h"

void KingGlory::goAtk()
{
	cout << "王者荣耀攻击我" << endl;
}

file of game2.c

#include"game2.h"

void LOL::goAtk()
{
	cout << "LOL攻击我" << endl;
}
#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
using namespace std;
#include "game1.h"
#include "game2.h"

// 1.命名空间用途:解决名称冲突

void test01()
{
	KingGlory::goAtk();
	LOL::goAtk();
}

//2.命名空间下可以放 变量 函数 结构体 类
namespace A
{
	int m_A;
	void func();
	struct Person
	{};

	class Animal
	{};
}

//3.命名空间 必须声明在全局作用域下
void test02()
{
	// namespace B{}    不可以命名到局部作用域
}

//4.命名空间可以嵌套命名空间
namespace B 
{
	int m_A = 10;
	namespace C
	{
		int m_A = 20;
	}

}

//5.命名空间是开放的,可以随时给命名空间添加新成员,重名的命名空间是进行合并操作,而不是覆盖操作

namespace B
{
	int m_B = 100;
}

void test04()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "B空间下的m_B = " << B::m_B << endl;
}
void test03()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "C空间下的m_A = " << B::C::m_A << endl;
}

//6.命名空间是可以匿名的
namespace
{
	int m_C = 1000;
	int m_D = 2000;
}

void test05()
{
	cout << "m_C" << m_C << endl;
	cout << "m_D" << m_D << endl;
}

//7.命名空间可以起别名

namespace longName
{
	int m_E = 10;
}

void test06()
{
	namespace LN = longName;
	cout << "longName:" << longName::m_E << endl;
	cout << "LN:" << LN::m_E << endl;
}

3. using statement and using compilation instruction

using statement

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

namespace KingGlory
{
	int sunwukongId = 1;
}

void test01()
{
 	cout << KingGlory::sunwukongId << endl;

	using KingGlory::sunwukongId;
	cout << sunwukongId << endl;
}

void test02()
{
	int sunwukongId = 2;
	//2.当using编译指令和就近原则同时出现的时候会优先使用就近原则
	using namespace KingGlory;
	cout << sunwukongId << endl;  
}

In test02, the principle of proximity is first used, so the output is that when the using statement and the principle of proximity appear at the same time, there is an error, try to avoid it

using compile directive

  1. using namespace KingGlory;
  2. When the using compilation directive and the proximity principle appear at the same time, the nearest is used first
  3. When there are multiple using compilation instructions, scope distinction needs to be added

4. C++ enhances C language

    1. Global variable detection enhancements
      1. int a ;
      2. int a = 10; ok under C, redefine in C++
    2. Enhanced function detection
      1. function return value
      2. parameter type
      3. The number of function call parameters
    3. Type conversion detection enhancements
      1. char * p = (char *)malloc(64) In C++, the left and right equal signs must be of the same type
    4. struct enhanced
      1. C++ can put functions in structures
      2. Creating a structure variable can simplify the keyword struct
    5. bool data type extension
      1. Only C++ has bool type
      2. Represents true --- 1 true false --- 0 false
      3. sizeof  = 1
    6. Ternary Operator Enhancement
      1. The value returned in C language
      2. In the C++ language, variables are returned
    7. const enhancement
      1. C language
        1. Global const Direct modification failed Indirect modification Syntax passed, operation failed
        2. local const direct modification failed indirect modification successful
      2. Under C++ language
        1. Global const is the same as C conclusion
        2. local const direct modification failed indirect modification failed  
        3. C++const can be called a constant 

4. const link attribute

  1. Global variables modified by const in C language are external link attributes by default
  2. The global variables modified by const in C++ are internal link attributes by default, and extern can be added to improve the scope
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;

//1、对const取地址,会分配零时内存
void test01()
{
	const int a = 10;
	int* p = (int*)&a;
}

//2、使用普通变量初始化const,可以修改 
void test02()
{
	int a = 10;
	const int b = 10;

	int* p =(int *) & b;
	*p = 1000;

	cout << "b = " << b << endl;
}

// 3、对于自定义数据类型

struct Person
{
	string m_name;
	int m_Age;
};

void test03()
{
	const Person p;
	Person * pp = (Person*)&p;
	pp->m_name = "Tom";
	pp->m_Age = 10;
}

5. Const allocation of memory

    1. Taking the address of a const variable will allocate temporary memory  
    2. Initialize const variables with normal variables

For custom data types

#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;

//1、对const取地址,会分配零时内存
void test01()
{
	const int a = 10;
	int* p = (int*)&a;
}

//2、使用普通变量初始化const,可以修改 
void test02()
{
	int a = 10;
	const int b = 10;

	int* p =(int *) & b;
	*p = 1000;

	cout << "b = " << b << endl;
}

// 3、对于自定义数据类型

struct Person
{
	string m_name;
	int m_Age;
};

void test03()
{
	const Person p;

	Person * pp = (Person*)&p;
	pp->m_name = "Tom";
	pp->m_Age = 10;
}


 

6. Try to use const instead of define

 Defined macro constants have no data type and do not pay attention to scope

7. Quote

  1.  Purpose: alias
    1. Syntax: type (must be consistent with original name type) & alias = original name
    2. References must be initialized
    3. Once a reference is initialized, it cannot refer to other variables
    4. Create a reference to the array
      1. direct reference
      2. int arr[10];
      3. int(&pArr)[10] = arr;
    5. Define the array type first, and then refer to it through the type definition
      1. typedef int(ARRAY_TYPE)[10];
      2. ARRAY_TYPE & pArr2 = arr;
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

//1.引用的基本作用是起别名 

void test01()
{
	int a = 10;
	int& b = a;
	b = 100;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}
void test02()
{
	int a = 10;
	// int &b;  引用必须初始化
	int& b = a;
	// 引用一旦初始化,就不可以引向其他变量

	int c = 100;
	b = c;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

void test03()
{
	//1、直接建立引用
	int arr[10]={0};
	int(&pArr)[10] = arr;

	for (int i = 0; i < 10; i++)
	{
		arr[i] = 100 + i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << pArr[i] << endl;
	}
}

The result of test01

 

 

The result of test02

 

 The result of test03

 

constant reference

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

void test01()
{
	//int &ref = 10;
	const int& ref = 10;  //加了const之后,相当于写成int temp = 10;const int &ref = temp
	int* p = (int*)&ref;
	*p = 1000;
	cout << ref << endl;
	
}
void  showValue(const int& a)
{
	cout << "a = " << a << endl;
}

//常量引用的使用场景:修饰函数的形参防止无操作
void test02()
{
	int a = 100;
	showValue(a);
	cout << "a = " << a << endl;
}

 

 

pointer reference

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
struct Person
{
	int age;
};
void allocateSpace(Person** p)
{
	//p指向 指针的指针 *p 指针指向的是person本体 **p person本体
	*p = (Person*)malloc(sizeof(Person));
	(*p)->age = 10;
}
void test01()
{
	Person* p = NULL;
	allocateSpace(&p);
	cout << "p.age = " << p->age << endl;
}
void allocateSpace2(Person*& pp) //Person * pp = p 
{
	pp = (Person*)malloc(sizeof(Person);
	pp->age = 20;
}
void test02()
{
	Person* p = NULL;
	allocateSpace(p);
	cout << "p.age = " << p->age << endl;
}

The result of test01

 The result of test02

 

Guess you like

Origin blog.csdn.net/qq_64691289/article/details/131503052