1.双冒号作用域运算符
1.1 ::的用途
1.2 代码示例
#include <iostream>
using namespace std;
int count=200;
void test()
{
int count=100;
cout<<"test count = "<<count<<endl;
cout<<"global count"<<::count<<endl;
}
int main()
{
test();
return 0;
}
2.命名空间的简单使用
2.1 命名空间的简单用途
namespace命名空间的用途:解决命名冲突的问题
2.2代码示例
以下新建test1.cpp,test1.h,test2.cpp,test2.h,show.cpp
//show.cpp
#include<iostream>
using namespace std;
#include "test1.h"
#include "test2.h"
int main()
{
TEST1::test();
TEST2::test();
return 0;
}
//test1.cpp
#include "test1.h"
void TEST1::test()
{
cout<<"test1"<<endl;
}
//test2.cpp
#include "test2.h"
void TEST2::test()
{
cout<<"test2"<<endl;
}
```cpp
//以下两行test1.h和test2.h都写
#include <iostream>
using namespace std;
//test1.h
namespace TEST1
{
void test();
}
//test1.h
namespace TEST2
{
void test();
}
如上可见用namespace定义命名空间的方法
namespace 命名空间名
{
//函数
//变量
…
}
命名空间下,可以放函数,变量,结构体,类,可以嵌套命名空间
例子
//demo.h
namespace demo
{
void func();
int m_a=10;
struct Person
{};
class A{};
namespace demo1
{
int m_b=20;
}
}
警告:命名空间必须定义在全局作用域下
嵌套命名空间的调用一样的写法,以demo为例
cout<<demo::demo1::m_b<<endl;
输出为20;
命名空间是开放的,可以随时在原先的命名空间里添加内容。不是覆盖而是拓展。
实例:与demo.h同一个文件
namespace demo
{
int m_c=100;
}
1.2.1无名,匿名命名空间
namespace
{
int m_d = 0;
int m_e = 0;
}
写无名命名空间,相当写了 static int m_d,static int m_e,只可以在当前文件使用,并没有什么用
命名空间可以起别名
namespace helloworld
{
int m_f = 0;
}
void test()
{
//起别名
namespace helloworld = hw;
}
3.explicit关键字
1.1首先问题导入一下
#include<iostream>
using namespace std;
class MyString
{
MyString(const char* str)
{
//
}
//重载
//explicit MyString(int a)
MyString(int a)
{
mSize = a;
}
char* mStr;
int mSize;
}
void test()
{
MyString str = 'abc';
MyString str2 = 10;//这行代码写的指向性非常low,不容易知道str2用作什么用途,
//隐式类型转换,MyString str2 = Mystring(10)
//**如果不想发生隐式类型转换,可以在MyString方法前加explicit关键字**
//加上explixit后Mystring str2 = 10;这行会报错
}
int main()
{
return 0;
}
3.2explicit的作用
防止隐式类型转换
5.好用的类型转换
1)static_cast<>() 静态类型转换,编译的时c++编译器会做类型检查;基本类型能转换 但是不能转换指针类型
2)若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释
3)一般性结论:
C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;
C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型 解释。总结:static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖
reinterpret_cast<>()很难保证移植性。
4)dynamic_cast<>(),动态类型转换,安全的基类和子类之间转换;运行时类型检查 5)const_cast<>(),去除变量的只读属性
5.2static_cast用法和reinterpret_cast用法
void main01()
{
double dPi = 3.1415926; //1静态的类型转换: 在编译的时 进行基本类型的转换 能替代c风格的类型转换 可以进行一部分检查
int num1 = static_cast<int> (dPi); //c++的新式的类型转换运算符
int num2 = (int)dPi; //c语言的 旧式类型转换
int num3=dPi;//隐士类型转换
cout << "num1:" << num1 << " num2:" << num2 << " num3:" << num3 << endl;
char *p1 = "hello wangbaoming " ;
int *p2 = NULL;
p2 = (int *)p1;
//2 基本类型能转换 但是不能转换指针类型
//p2 = static_cast<int *> (p1);
//“static_cast”: 无法从“char *”转换为“int *”
//3 可以使用 reinterpret_cast 进行重新解释
p2 = reinterpret_cast<int *> (p1);
cout << "p1 " << p1 << endl; cout << "p2 " << p2 << endl;
//4 一般性的结论: c语言中 能隐式类型转换的 在c++中可以用 static_cast<>()进行类型转换
//C++编译器在编译检查一般都能通过
//c语言中不能隐式类型转换的,在c++中可以reinterpret_cast<>() 进行强行类型 解释
system("pause");
return ;}
5.3dynamic_cast用法和reinterpret_cast用法
class Animal
{
public:
virtual void cry() = 0;
};
class Dog : public Animal
{
public:
virtual void cry()
{
cout << "wangwang " << endl;
}
void doSwim()
{
cout << "我要狗爬" << endl;
}
};
class Cat : public Animal
{
public:
virtual void cry()
{
cout << "miaomiao " << endl;
}
void doTree()
{
cout << "我要爬树" << endl;
}
};
class Book
{
public:
void printP()
{
cout << price << endl;
}
private:
int price;
};
void ObjPlay(Animal *base)
{
base->cry();
Dog *pDog = dynamic_cast<Dog *>(base);
if (pDog != NULL) {
pDog->cry();
pDog->doSwim();
}
Cat *pCat = dynamic_cast<Cat *>(base);
if (pCat != NULL) {
pCat->cry();
pCat->doTree();
}
}
void main02(){
Animal *base = NULL;
//1 可以把子类指针赋给 父类指针 但是反过来是不可以的 需要 如下转换
//pdog = base;
Dog *pDog = static_cast<Dog *> (base);
//2 把base转换成其他 非动物相关的 err
//Book *book= static_cast<Book *> (base);
//3 reinterpret_cast //可以强制类型转换
Book *book2= reinterpret_cast<Book *> (base);
//4 dynamic_cast用法
ObjPlay(new Cat());
system("pause");
}
const_cast用法
//典型用法 把形参的只读属性去掉
void Opbuf(const char *p){
cout << p << endl;
char *p2 = const_cast<char*>(p);
p2[0] = 'b';
cout << p << endl;
}
void main(){
const char *p1 = "11111111111";
char *p2 = "22222222";
char *p3 = const_cast<char *>(p1);
char buf[100] = "aaaaaaaaaaaa";
Opbuf(buf); //要保证指针所执行的内存空间能修改才行 若不能修改 还是会引起程序异常 //Opbuf("dddddddddddsssssssssssssss");
system("pause");}