C++经典问题

C++经典问题,第1张

文章目录
      • 一. 声明
      • 二. 命名空间
      • 三. 类型别名
      • 四. 改变从基类继承来的成员的访问级别.
      • 五. 函数指针别名
      • 六. 模板别名
        • ① 容器模板
        • ② 函数指针模板

一. 声明
  1. 声明的对象或者变量可以直接在当前的作用域内使用.
  2. 语法: using 名称,例如 using std::cout
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/


#include 
using std::cout; // using声明,表示cout这个对象是可以使用的.类似的cin就不能直接使用


int main()
{
	// cout被声明过所以可以直接使用,但是endl没有被声明要加上std才能访问
	cout << "我被using声明过,可以直接使用" << std::endl;
	int i;
	// cin >> i; // 因为cin没有被声明不能直接使用
	std::cin >> i; // 必须加上std限定符号

	system("pause");
	return 0;
}

二. 命名空间
  1. using naemspace 命名空间名
  2. 我们可以在当前作用域内使用命名空间中的变量,命名空间中的所有的变量都是可见的.
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include 
using namespace std; // 命名空间std中的所有的变量名称都是可见的

int main()
{
	cout << "我是命名空间std中的变量,std被using 声明过!" << endl;
	int i;
	cin >> i; // cin 也是命名空间std中的变量,可以直接使用!

	system("pause");
	return 0;
}
三. 类型别名
  1. using 可以代替typedef 给一个类型起别名
  2. using 类型别名 = 原类型
// 重定义unsigned int
typedef unsigned int uint;
using uint = unsigned int;
// 重定义 std::map
typedef std::map<std::string,int> map_int_t;
using map_int_t = std::map<std::string,int>;
四. 改变从基类继承来的成员的访问级别.
  1. 可以在派生类中使用using关键字指定调用父类的成员.
  2. 指定的时候放到public或者是其他的访问限定符的作用域内,就可以改变这个成员的访问级别
  3. using不指定参数列表语法就是: using Base::somefunction;
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/


#include 
using namespace std;
class Base
{
public:
	int func(int x) {};
	int func_02(int x) {};
	int b;
};

class Derived :private Base
{
	// 因为是私有继承,所以基类的成员都会变成是私有的,如果要更改属性的访问级别,可以使用public
public:
	using Base::func; //func(int x) 本来继承过来的应该是私有的,现在我们把它变成public的了,注意:using不指定参数列表
	using Base::b;
};

int main()
{
	Base b;
	Derived d;
	d.func(10); // 这类可以调用的原因是使用using改变了func的属性
	//d.func_02(); // 这里就会报错,不可访问,因为是私有继承,继承过来的函数是私有的,类外不可访问.
	system("pause");
	return 0;
}
五. 函数指针别名

typedef void (*funcPtr)(int,int);
using funcPtr = void (*)(int,int)

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include 
using namespace std;
int add(int a, int b)
{
	return a + b;
}

int main()
{
	// 使用函数指针
	int(*funcPtr1)(int, int) = add;
	cout << funcPtr1(10, 10) << endl;

	// 使用typedef 注意funcPtr2是一个函数指针的类型
	typedef int(*funcPtr2)(int, int);
	funcPtr2 func = add; 
	cout << func(10, 10) << endl;

	// 使用using
	using funcPtr3 = int(*)(int, int);
	funcPtr3 func2 = add;
	cout << func2(10, 10) << endl;
	system("pause");
	return 0;
}

六. 模板别名 ① 容器模板
template <typename T>
using ArenaDeque = std::deque<T, ArenaAllocatorAdapter<T>>;

template <typename T>
using ArenaQueue = std::queue<T, ArenaDeque<T>>;

template <typename T>
using ArenaVector = std::vector<T, ArenaAllocatorAdapter<T>>;

template <typename T, typename Comparator = std::less<T>>
using ArenaSet = std::set<T, Comparator, ArenaAllocatorAdapter<T>>;

template <typename K, typename V, typename Comparator = std::less<K>>
using ArenaSafeMap = 
SafeMap<K, V, Comparator, ArenaAllocatorAdapter<std::pair<**const** K, V>>>;

typedef 和using在模板别名中的区别?
应该优先使用using而不是typedef类给模板取别名
如果我们需要一个固定以std::string为key的map,它可以映射到int或者另外一个std::string.如果通过typedef来实现:

template <typename Val>
struct strMap
{
	typedef std::map<std::string,Val> type;
};
strMap<int>::type map1; // 使用的时候必须这么用,很麻烦并且不是那么容易明白在干什么

使用using来实现

template <typename Val>
using strMapT = std::map<std::string,Val>;
strMapT<int> map;// 只需要指定参数即可,不用写冒号::,也不用另外一定义一个结构体
② 函数指针模板

using定义模板别名的语法,只是加上templaet 即可,使用的时候加上参数即可.

// 使用typedef
template <typename T>
struct funcT
{
	typedef void (*FUNC_PTR)(T,T);
};
FUNC_PTR<int,int>::type func1; // 使用

// 使用using
template <typename T>
using FUNC_PTR = void (*)(T,T);
FUNC_PTR<int,int> func2; // 使用

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/562883.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-02
下一篇2022-04-02

发表评论

登录后才能评论

评论列表(0条)

    保存