1.作用域限定符
通常情况下,如果有两个同名变量,一个是全局变量,另一个是局部变量,那么局部变量在其作用域内具有较高的优先权,它将屏蔽全局变量。
int a = 10;//全局变量
void test()
{
//局部变量
int a = 20;
//全局a被隐藏
cout << "a:" << a << endl; // 20
//打印局部变量a
cout << "局部变量a:" << a << endl;
//打印全局变量a
cout << "全局变量a:" << ::a << endl; //用::对被屏蔽的同名的全局变量进行访问。
}
2.命名空间
c语言可以通过static关键字来使得名字只得在本编译单元内可见,在c++中我们将通过一种通过命名空间来控制对名字的访问。
程序员使用各种各样的C++库时,标识符的命名发生冲突,标准C++引入关键字namespace(命名空间/名字空间/名称空间),可以更好地控制标识符的作用域。
名称(name)可以是符号常量、变量、函数、结构、枚举、类和对象等等。
举例
namespace ns2//定义一个名为ns2的命名空间 { double a;//(1)变量 const int PI=3.14;//(2)常量 int fun1(){ ... return 0;} //(3)函数 typedef struct Mat{int b[][];}Mat;//(4)结构体 Class A{ public: };//(5)类 template <typename T>//(6)函数模板 int fun2(T c){ ... return 0;} namespace ns3{int d;}//(7)嵌套的命名空间 }
- 定义
///命名空间只能全局范围内定义(以下错误写法)
void test()
{
namespace A //error在函数内部定义
{
int a = 10;
}
namespace B
{
int a = 20;
}
}
- 命名空间可以嵌套
namespace A //全局下定义
{
int a = 10;
namespace B
{
int a = 20;
}
}
- 命名空间是开放的,即可以随时把新的成员加入已有的命名空间中
namespace A
{
int a = 10;
}
namespace A //新加入一个函数
{
void func()
{
cout << "hello namespace!" << endl;
}
}
void test()
{
cout << "A::a : " << A::a << endl;
A::func();//测试调用
}
-
声明和实现可分离
#pragma once//防止重复包含头文件 namespace MySpace { void func1();//申明 void func2(int param); } void MySpace::func1() { cout << "MySpace::func1" << endl;//定义 } void MySpace::func2(int param) { cout << "MySpace::func2 : " << param << endl;//测试调用 }
-
无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static,使得其可以作为内部连接
//没有起名字 namespace{ int a = 10; void func() { cout << "hello namespace" << endl; } } void test() { cout << "a : " << a << endl; func(); }
-
命名空间别名 (一个给另一个复制)
-
namespace veryLongName{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test(){
namespace shortName = veryLongName;
cout << "veryLongName::a : " << shortName::a << endl;
veryLongName::func();
shortName::func();
}
3. using指令
- using声明可使得指定的标识符可用。using A::paramA; 具体到某个变量
namespace A
{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcA" << endl; }
}
void test()
{
//1. 通过命名空间域运算符
cout << A::paramA << endl;
A::funcA();
//2. using声明
using A::paramA;
using A::funcA;
cout << paramA << endl;
//cout << paramB << endl; //不可直接访问 未强调使用
//error: 'paramB' was not declared in this scope
funcA();
//3. 同名冲突
//int paramA = 40; //相同作用域注意同名冲突
//error: redeclaration of 'int paramA'|
cout << paramA << endl;
}
- 命名空间函数重载
//如果命名空间包含一组用相同名字重载的函数,using声明就声明了这个重载函数的所有集合。
namespace A{
void func(){}
void func(int x){}
int func(int x,int y){}
}
void test()//编译通过
{
using A::func;
func();
func(10);
func(10, 20);
}
- using编译指令 using namespace A; 使用这个命名空间,using编译指令使整个命名空间标识符可用.
//使用时要注意不同空间的同名变量之间产商的二义性
namespace A{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcB" << endl; }
}
void test01(){
using namespace A;
cout << paramA << endl;
cout << paramB << endl;
funcA();
funcB();
//不会产生二义性
int paramA = 30;
cout << paramA << endl; //使用本代码块局部变量
}
namespace B{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcB" << endl; }
}
void test02(){
using namespace A;
using namespace B;
//二义性产生,不知道调用A还是B的paramA
//cout << paramA << endl;
}
4.全局变量检测增强
int a = 10; //赋值,当做定义
int a; //没有赋值,当做声明
int main()
{
printf("a:%d\n",a);
return EXIT_SUCCESS;
}
//此代码在c++下编译失败,在c下编译通过.
5. C++所有变量函数都必须有类型
//i没有写类型,可以是任意类型
int fun1(i)//error: 'i' was not declared in this scope|
{
printf("%d\n", i);
return 0;
}
//i没有写类型,可以是任意类型
int fun2(i)//error: 'i' was not declared in this scope|
{
printf("%s\n", i);
return 0;
}
//没有写参数,代表可以传任何类型的实参
int fun3(){
printf("fun33333333333333333\n");
return 0;
}
//C语言,如果函数没有参数,建议写void,代表没有参数
int fun4(void){
printf("fun4444444444444\n");
return 0;
}
g()//error: ISO C++ forbids declaration of 'g' with no type [-fpermissive]|
{
return 10;
}
int main(){
fun1(10);//error: 'fun1' cannot be used as a function|
fun2("abc");//error: 'fun2' cannot be used as a function|
fun3(1, 2, "abc");//error: too many arguments to function 'int fun3()'|
printf("g = %d\n", g());
return 0;
}
//以上c代码c编译器编译可通过,c++编译器无法编译通过。
6.更严格的类型转换
typedef enum COLOR{ GREEN, RED, YELLOW } color;
int main()
{
color mycolor = GREEN;
mycolor = 10;//error: invalid conversion from 'int' to 'color {aka COLOR}' [-fpermissive]|
printf("mycolor:%d\n", mycolor);//error: invalid conversion from 'void*' to 'char*' [-fpermissive]|
char* p = malloc(10);
return 0;
}
7.struct加强
- c中定义结构体变量需要加上struct关键字,c++不需要。
- c中的结构体只能定义成员变量,不能定义成员函数。c++即可以定义成员变量,也可以定义成员函数。
//1. 结构体中即可以定义成员变量,也可以定义成员函数
struct Student
{
string mName;
int mAge;
void setName(string name){ mName = name; }
void setAge(int age){ mAge = age; }
void showStudent()
{
cout << "Name:" << mName << " Age:" << mAge << endl;
}
};
//2. c++中定义结构体变量不需要加struct关键字
void test01()
{
struct Student student;//c语言写法
Student student;//c++可省略
student.setName("John");
student.setAge(20);
student.showStudent();
}
8. bool类型(三个关键字 bool true false)
void test()
{
cout << sizeof(false) << endl; //为1,//bool类型占一个字节大小
bool flag = true; // c语言中没有这种类型
flag = 100; //给bool类型赋值时,非0值会自动转换为true(1),0值会自动转换false(0)
}
9. 三目运算符
// C语言返回值 C++返回变量(可以当左值)
void test()
{
int a = 10;
int b = 20;
printf("ret:%d\n", a > b ? a : b);
//思考一个问题,(a > b ? a : b) 三目运算表达式返回的是什么?
cout << "b:" << b << endl;
//返回的是左值,变量的引用
(a > b ? a : b) = 100;//返回的是左值,变量的引用
cout << "b:" << b << endl;
}