自定义命名空间
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace sss {
int g_a = 10;
}
int main(void)
{
using namespace sss;
cout << g_a << endl;
}
命名空间的嵌套
```cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace sss {
int g_a = 10;
}
namespace a {
int aa = 20;
namespace b {
struct teacher
{
int id;
char name[64];
};
}
}
int main(void)
{
using namespace sss;
cout << g_a << endl;
using namespace a::b;
struct teacher t1;
t1.id = 10;
return 0;
}
结构体
#include "stdafx.h"
#include <iostream>
using namespace std;
struct student {
int id;
char name[64];
};
void test()
{
struct student s1;
s1.id = 20;
}
int main()
{
test();
}
关于const
const int * c;//修饰变量
c是一个指向常整形数的指针,所指向的内存数据不能改变,但指针本身可以修改
( 这个***修饰的是const int )
int *const d; //修饰指针
常指针(指针变量不能修改,但它所指向的内存空间可以被修改)
const int * const e;
两者都不可修改
a是常量
const int a =10;//编译阶段
无地址
#define A 10 预处理阶段
int p=[int]**&a
*p=20
对常量取地址,编译器会临时开辟一个空间temp,让这个指针存放这个临时空间的地址
*p=20 改变的不是a,改变的是临时开辟的变量,与a无关。
引用
引用一经声明,不可改变。
此种为错误,引用一定要初始化。
int &re3=re
// h_mingcheng.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void change(int *p)
{
*p = 100;
}
void change2(int &r)//r为a的别名
{
r = 1000;
}
int main()
{
int a = 20;
int b = 30;
int *p = &a;
*p = 30;
p = &b;
*p = 20;
int &re = a;
re = 50;
re = b;
re = 50;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
int &re2 = b;
re2 = a;
int re3 = re;
re3 = 1000;
cout << "re3" << re3 << endl;
cout << "re" << re << endl;
cout << "a" << a <<endl;
change(&a);
cout << "a=" << a << endl;
change2(a);
cout << "change2 a=" << a << endl;
}
参数为结构体
// h_mingcheng.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct student {
int id;
char name[64];
};
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void my_swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void printdd(struct student *s)
{
cout << s->id << s->name << endl;
}
int main()
{
int a = 10;
int b = 20;
my_swap(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
student s1 = { 10,'sss' };
printdd(&s1);
return 0;
}
引用的本质
引用所占的大小与指针相同
常量要初始化,引用也要初始化
int *const p = &a;
引用可能是一个常指针
引用作为函数返回值
// h_mingcheng.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
char * getmem(int num)
{
char *p = NULL;
p = (char*)malloc(num);
return p;
}
int getum1(char **pp, int num)
{
char *p = NULL;
p = (char*)malloc(num);
*pp = p;
return 0;
}
int getA1()
{
int a = 10;
return a;
}
int getA2(int *a)
{
//int a = 10;
*a = 10;
return 0;
}
//引用作为返回值,不要返回局部变量的引用
int& getA3()//返回引用类型
{
int a = 10;
return a;
}//int &temp=a
int &getA4()
{
static int a = 10;
return a;
}
int main()
{
int a = 0;
char *pp = NULL;
a = getA1();
pp = getmem(10);
cout << "bbbbbbbbbbbbbbbbbbb" << endl;
int main_a = 0;
main_a = getA3();//main_a=temp
cout << "main_a" << main_a << endl;
cout << "bbbbbbbbbbbbbbbbbbb" << endl;
int &main_a_re = getA3();//*(getA3()) *temp//避免 getA3返回的就是局部变量a的引用,就是返回a本身,temp就是引用类型,
//main_a_re就是a了
cout << "main_a_re" << main_a_re << endl;
cout << "main_a_re" << main_a_re << endl;
int &main_b = getA4();//此时main_b成为a的别名
cout << "main_b" << main_b << endl;
getA4() = 1000;//因为getA4返回的是别名,就是返回static int a,就可以当左值了
return 0;
}
指针引用
// h_mingcheng.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct teacher
{
int id;
char name[64];
};
int get_mem(struct teacher** tpp)
{
struct teacher *tp = NULL;
tp = (struct teacher*)malloc(sizeof(struct teacher));
if (tp == NULL)
{
return -1;
}
tp->id = 100;
strcpy_s(tp->name,"fff");
*tpp = tp;
return 0;
}
void free_teacher(struct teacher **tpp)
{
if (tpp == NULL)
return;
struct teacher *tp = *tpp;
if (tp != NULL)
{
free(tp);
*tpp = NULL;
}
}
void free_mem2(struct teacher * &tp)
{
if (tp != NULL)
{
free(tp);
tp = NULL;
}
}
int get_mem2(struct teacher* &tp)//int &tp
{
tp = (struct teacher *)malloc(sizeof(struct teacher));
if (tp == NULL)
{
return -1;
}
tp->id = 300;
strcpy_s(tp->name, "kkkk");
return 0;
}
int main()
{
struct teacher *tp = NULL;
get_mem(&tp);
cout << "id" << tp->id << " " << tp->name << endl;
free_teacher(&tp);
get_mem2(tp);
cout << "id" << tp->id << " " << tp->name << endl;
}