C/C++ 命名空间引用知识

标准命名空间

命名空间的使用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

// 命名空间必须定义在全局的作用域下
// 命名空间可以嵌套命名空间
// 如果命名空间冲突,则会自动的合并为一个命名空间

namespace zh {
	int m_id = 10;
	void Display(){ cout << "Display zh func" << endl; }
}

namespace cn {
	int m_id = 20;

	namespace cpp {
		int m_id = 30;
	}
}

// 当写了无名空间,相当于写了 static int m_ptr;
// 静态变量只能在当前文件中使用
namespace{ int m_ptr = 10;  }

int main(int argc, char* argv[])
{
	using namespace zh;
	cout << zh::m_id << endl;
	cout << cn::m_id << endl;
	cout << cn::cpp::m_id << endl;
	zh::Display();

	system("pause");
	return 0;
}

C++ 引用知识

引用的实质就是取别名,&写到左侧叫引用,写到右侧叫取地址。

普通的引用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{

	int a = 10;
	int &b = a;   // 引用  

	cout << b << endl;

	system("pause");
	return 0;
}

数组引用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{

	int ary[10];
	for (int x = 0; x < 10; x++)
		ary[x] = x;

	// 给数组进行取别名
	int(&parr)[10] = ary;

	// 用别名来输出
	for (int y = 0; y < 10; y++)
	{
		cout << parr[y] << " " ;
	}

	// 第二种方式取别名
	typedef int(ARYS)[10];  // 一个具有10个元素的数组

	ARYS &pary2 = ary;
	for (int y = 0; y < 10; y++)
	{
		cout << pary2[y] << " ";
	}

	system("pause");
	return 0;
}

参数传递方式 参数传递两种方式,传递数值,传递地址,还可以传递引用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

// 指针传递
void mySwap(int *a, int *b)
{
	int tmp = *a;

	*a = *b;
	*b = tmp;
	cout << *a << *b << endl;
}

// 引用传递
void myswap2(int &a, int &b)
{
	int tmp = a;
	a = b;
	b = tmp;
}


int main(int argc, char* argv[])
{
	int x = 10;
	int y = 20;

	myswap2(x,y);

	cout << x << endl;

	system("pause");
	return 0;
}

引用传递注意事项 int a = 10; 改成static 即可实现不销毁。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

int& dowork()
{
	int a = 10; // 局部变量,返回前销毁了,引用就不存在了
	return a;
}


int main(int argc, char* argv[])
{
	int &ret = dowork();  // 编译器做了优化,如果打印多次就会出现乱码

	cout << ret << endl;  // 10
	cout << ret << endl; // 乱码了
	cout << ret << endl;
	cout << ret << endl;
	cout << ret << endl;

	// 引用可以作为左值使用
	dowork() = 1000; // 相当于写了 a = 1000;

	system("pause");
	return 0;
}

引用的本质: 引用所占用的内存地址与指针类似,引用本质就是指针常量

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

void test(int& ref)
{
	ref = 1000;  
}

int main(int argc, char* argv[])
{

	int a = 10;
	int& aref = a; // 指针常量必须初始化,内部是一个指针常量

	aref = 20;  // 地址不可改

	cout << a << endl;  // 自动转为 *aref = 20;

	cout << aref << endl;

	system("pause");
	return 0;
}

引用使用场景:指针的引用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>

using namespace std;

struct person
{
	int m_age;

};

// 分配空间函数
// **p = > 具体的 person 对象
// *p => 对象的指针
// p => 指针的指针
void allocat(person **p)
{
	*p = (person *) malloc(sizeof(person));
	(*p)->m_age = 100;
	
}
// 普通开辟空间写法
void test()
{
	struct person *p = NULL;
	allocat(&p);  // 开始分配内存
	cout << "p的:" << p->m_age << endl;
}

// ------------------------------------------------
// 利用指针引用开辟空间
void allocatbyref(person* &p)
{
	p = (person *)malloc(sizeof(person));
	p->m_age = 1000;
}
void test2()
{
	person *p = NULL;
	allocatbyref(p);
	cout << (*p).m_age << endl;
}

int main(int argc, char* argv[])
{

	test2();
	system("pause");
	return 0;
}

常量引用:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>

using namespace std;

void test()
{
	const int &ref = 10;  // 加入const后,编译器会处理,创建了一个临时变量,int tmp = 10; const int &ref = tmp;
	// ref = 10;  无法直接修改

	// 换种方式修改它
	int *p = (int*)&ref;
	*p = 1000;
	cout << ref << endl;
}

int main(int argc, char* argv[])
{
	test();
	system("pause");
	return 0;
}

常量引用使用场景: 用来修饰型can

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>

using namespace std;

void test(const int &val)  // 不让你修改下
{

	cout << val << endl;
}


int main(int argc, char* argv[])
{
	int a = 10;

	test(a);
	system("pause");
	return 0;
}

函数与类的一些知识点

内联函数,为了替换c中的宏,内联函数本身也是函数,只是加了一个关键字inline()

编译器会如何除了? 直接变成了宏,直接内嵌到调用的位置,并不增加新的函数。

在类中定义的,成员函数,默认会自动加上内联函数关键字声明,这是编译器为我们加上的。

但是,如果函数过大,或使用了循环等结构,则类则不会声明为内联函数。

#include <iostream>
#include <string>

using namespace std;

inline void compare(int x, int y)
{
	int ret = x < y ? x : y;
	cout << ret << endl;
}

int main(int argc, char *argv[])
{
	compare(10, 20);
	system("pause");
	return 0;
}

函数的默认参数

#include <iostream>
#include <string>

using namespace std;

void func(int x = 10, int y = 20)
{
	cout <<x+y << endl;
}

int main(int argc, char *argv[])
{
	func(1, 20);
	system("pause");
	return 0;
}

函数重载: 名称相同,但是符号不同,后面的参数不同,就是重载。

当使用重载时,编译器会偷偷在前面加上 _func 关键字,这样来使用重载,通过编译检查。

using namespace std;

void func(int x)
{
	cout <<x << endl;
}

void func(double x)
{
	cout << x << endl;
}

int main(int argc, char *argv[])
{
	func(1);
	func(1.1);
	system("pause");
	return 0;
}

extern c 的作用: 在c++ 中想要使用C代码,需要加上extern 关键字。

#include <iostream>
#include <string>

using namespace std;

extern "C" void show()
{
	printf("aaaaa \n");
}

int main(int argc, char *argv[])
{
	show();
	system("pause");
	return 0;
}

类的基本定义

#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
	int uid;
	char *name;
	int age;

public:
	void set(int id,char *name,int age)
	{
		this->uid = id;
		this->name = name;
		this->age = age;
	}
	void display()
	{
		cout << this->uid << this->name << this->age << endl;
	}
};

int main(int argc, char *argv[])
{
	Student stu;   // 实例化

	stu.set(1001, "lyshark", 23);                     // 设置数据
	cout << stu.uid << stu.name << stu.age << endl;   // 输出
	stu.display();                                    // 使用成员函数输出

	Student *ptr = &stu;           // 使用指针
	cout << ptr->uid << ptr->name << ptr->age << endl;

	system("pause");
	return 0;
}

C语言实现的类: 这个是不是看起来很麻烦了,我们继续改进。

#include <iostream>
#include <string>

using namespace std;

struct Student
{
	char name[64];
	int age;
};

void PrintPerson(struct Student *ptr)
{
	printf("Name: %s --> Age: %d \n", ptr->name, ptr->age);
}

int main(int argc, char *argv[])
{
	struct Student stu;

	strcpy(stu.name, "lyshark");
	stu.age = 23;
	PrintPerson(&stu);

	system("pause");
	return 0;
}
文章出处:https://www.cnblogs.com/LyShark/p/12869665.html
版权声明:本博客文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!

如果您恶意转载本人文章并被本人发现,则您的整站文章,将会变为我的原创作品,请相互尊重 !
转载规范 点击阅读 如果您转载本人文章,则视为您默认同意此规范约定。
原文地址:https://www.cnblogs.com/LyShark/p/12869665.html