1:using声明(引入单个名称)
using声明是将命名空间中某个名字单独引入到当前作用域,这使得我们在当前作用域下可以直接使用该名字而无需使用作用域限定符 :: 。
#include
using std::string;
int main() {
string c = "sss";
return 0;
}
2: 使用别名, using类型别名 = 原类型。
#include
using std::string;
using uint = unsigned int;
int main() {
string c = "sss";
uint i= 1;
return 0;
}
3:using声明可以改变 派生类对父类成员的访问控制。
#include
#include
using namespace std;
class Base
{
protected:
int bn1;
int bn2;
};
class Derived :private Base {
public:
// 在当前作用域中引入了 父类的保护成员,在当前作用域中是可以访问的。
using Base::bn1;
};
class DerivedAgain :public Derived {
};
int main() {
Derived d;
DerivedAgain da;
d.bn1 = 1;
// d.bn2 = 2; // error : bn2 是Base里一个私有的成员
da.bn1 = 3;
cout |