C++ 中using 的使用

文章来源: https://blog.csdn.net/shift_wwx/article/details/78742459

https://blog.csdn.net/wishchin/article/details/79870177


前言:

今天在看vector.h的时候,碰到一个using的奇怪用法,才疏学浅之前没有碰到过,整理一下。

 

来看下source code:

template<class _Ty,
	class _Alloc = allocator<_Ty>>
	class vector
		: public _Vector_alloc<_Vec_base_types<_Ty, _Alloc>>
	{	// varying size array of values
private:
	using _Mybase = _Vector_alloc<_Vec_base_types<_Ty, _Alloc>>;
	using _Alty = typename _Mybase::_Alty;
	using _Alty_traits = typename _Mybase::_Alty_traits;
 
public:
	static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same<_Ty, typename _Alloc::value_type>::value,
		_MISMATCHED_ALLOCATOR_MESSAGE("vector<T, Allocator>", "T"));
 
	using value_type = _Ty;
	using allocator_type = _Alloc;
	using pointer = typename _Mybase::pointer;
	using const_pointer = typename _Mybase::const_pointer;
	using reference = _Ty&;
	using const_reference = const _Ty&;
	using size_type = typename _Mybase::size_type;
	using difference_type = typename _Mybase::difference_type;
	using iterator = typename _Mybase::iterator;
	using const_iterator = typename _Mybase::const_iterator;
	using reverse_iterator = _STD reverse_iterator<iterator>;
	using const_reverse_iterator = _STD reverse_iterator<const_iterator>;

下面来整理using的三种用法。


1、命名空间的使用

一般为了代码的冲突,都会用命名空间。例如,对于Android代码会使用Android作为命名空间。

namespace android;

在code中使用的时候可以用android::加具体的类方法。也可以直接使用using namespace android;

具体的命名空间使用方法不做过多说明。

2、在子类中引用基类的成员

来看下source code:

class T5Base {
public:
    T5Base() :value(55) {}
    virtual ~T5Base() {}
    void test1() { cout << "T5Base test1..." << endl; }
protected:
    int value;
};
 
class T5Derived : private T5Base {
public:
    //using T5Base::test1;
    //using T5Base::value;
    void test2() { cout << "value is " << value << endl; }
};

基类中成员变量value是protected,在private继承之后,对于外界这个值为private,也就是说T5Derived的对象无法使用这个value。
如果想要通过对象使用,需要在public下通过using T5Base::value来引用,这样T5Derived的对象就可以直接使用。

同样的,对于基类中的成员函数test1(),在private继承后变为private,T5Derived的对象同样无法访问,通过using T5Base::test1 就可以使用了。

注意,using只是引用,不参与形参的指定。

3、别名指定

这点就是最开始看到的source code。在C++11中提出了通过using指定别名。

例如上面source code 中:

using value_type = _Ty

以后使用value_type value; 就代表_Ty value;

这个让我们想起了typedef,using 跟typedef有什么区别呢?哪个更好用些呢?

例如:

typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UPtrMapSS;

而C++11中:

using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;

或许从这个例子中,我们是看不出来明显的好处的(而于我来说,以一个第三者的角度,这个例子也难以说服我一定要用C++11的using)。
再来看下:

typedef void (*FP) (int, const std::string&);

若不是特别熟悉函数指针与typedef的童鞋,我相信第一眼还是很难指出FP其实是一个别名,代表着的是一个函数指针,而指向的这个函数返回类型是void,接受参数是int, const std::string&。那么,让我们换做C++11的写法:

using FP = void (*) (int, const std::string&);

我想,即使第一次读到这样代码,并且知道C++11 using的童鞋也能很容易知道FP是一个别名,using的写法把别名的名字强制分离到了左边,而把别名指向的放在了右边,比较清晰。


而针对这样的例子,我想我可以再补充一个例子:

typedef std::string (Foo::* fooMemFnPtr) (const std::string&);
 
using fooMemFnPtr = std::string (Foo::*) (const std::string&);

从可读性来看,using也是要好于typedef的。

那么,若是从可读性的理由支持using,力度也是稍微不足的。来看第二个理由,那就是举出了一个typedef做不到,而using可以做到的例子:alias templates, 模板别名。

template <typename T>
using Vec = MyVector<T, MyAlloc<T>>;
 
// usage
Vec<int> vec;

这一切都会非常的自然。


那么,若你使用typedef来做这一切:

template <typename T>
typedef MyVector<T, MyAlloc<T>> Vec;
 
// usage
Vec<int> vec;

当你使用编译器编译的时候,将会得到类似:error: a typedef cannot be a template的错误信息。

那么,为什么typedef不可以呢?在 n1449 中提到过这样的话:"we specifically avoid the term “typedef template” and introduce the new syntax involving the pair “using” and “=” to help avoid confusion: we are not defining any types here, we are introducing a synonym (i.e. alias) for an abstraction of a type-id (i.e. type expression) involving template parameters." 所以,我认为这其实是标准委员会他们的观点与选择,在C++11中,也是完全鼓励用using,而不用typedef的。
 

具体的可以看下Effective Modern C++

 

参考:

https://zhuanlan.zhihu.com/p/21264013


https://blog.csdn.net/wishchin/article/details/79870177

一、《Effective Modern C++》里有比较完整的解释

各个作用

/*定义别名*/
 template<class T>
 using Tlist = std::list<T>;
 
 using Tlist = std::list<char>;
 Tlist listChar;
 
 //typedef void (*df)()
  using  df = void(*)();
 /*使用外部构造*/
 using A::A;
 
 /*引用外部类型*/
using typename A;

二、Using 关键字的作用:重载父类函数

1.在当前文件中引入命名空间

     这是我们最熟悉的用法,例如:using namespace std;

 

2.在子类中使用 using 声明引入基类成员名称(参见C++ primer)

在private或者protected继承时,基类成员的访问级别在派生类中更受限:

class Base {
public:
std::size_t size() const { return n; }
protected:
std::size_t n;
};
class Derived : private Base { . . . };

在这一继承层次中,成员函数 size 在 Base 中为 public,但在 Derived 中为 private。为了使 size 在 Derived 中成为 public,可以在 Derived 的 public
部分增加一个 using 声明。如下这样改变 Derived 的定义,可以使 size 成员能够被用户访问,并使 n 能够被 Derived的派生类访问:

class Derived : private Base {
public:
using Base::size;
protected:
using Base::n;
// ...
};

另外,当子类中的成员函数和基类同名时,子类中重定义的成员函数将隐藏基类中的版本,即使函数原型不同也是如此(隐藏条件见下面)。

如果基类中成员函数有多个重载版本,派生类可以重定义所继承的 0 个或多个版本,但是通过派生类型只能访问派生类中重定义的那些版本,所以如果派生类想通过自身类型使用所有的重载版本,则派生类必须要么重定义所有重载版本要么一个也不重定义。有时类需要仅仅重定义一个重载集中某些版本的行为,并且想要继承其他版本的含义,在这种情况下,为了重定义需要特化的某个版本而不得不重定义每一个基类版本,可能会令人厌烦。可以在派生类中为重载成员名称提供 using 声明(为基类成员函数名称而作的 using 声明将该函数的所有重载实例加到派生类的作用域),使派生类不用重定义所继承的每一个基类版本。一个 using 声明只能指定一个名字,不能指定形参表,使用using声明将名字加入作用域之后,派生类只需要重定义本类型确实必须定义的那些函数,对其他版本可以使用继承的定义。

 

“隐藏”是指派生类的函数屏蔽了与其同名的基类函数,规则如下:

1、如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)

2、如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)

 

#include "StdAfx.h"
#include <iostream>
using namespace std;
class Base
{
public:    
   void menfcn()
  {
     cout<<"Base function"<<endl; 
  }
    void menfcn(int n)
    {
     cout<< cout<<"Base function with int"<<endl; 
    }
};
 
class Derived : Base
{
public:    
using Base::menfcn;//using声明只能指定一个名字,不能带形参表    
int menfcn(int)
{ cout<< cout<<"Derived function with int"<<endl; }
};
int main()
{    Base b; 
     Derived d;   
  b.menfcn();   
  d.menfcn();//如果去掉Derived类中的using声明,会出现错误:error C2660: 'Derived::menfcn' : function does not take 0 arguments    std::cin.ignore(std::cin.gcount()+1);//清空缓冲区    std::cin.get();//暂停程序执行  
}

三、需要注意的情况

子类中using引入基类函数时需要注意的情况

class base{
public:
 void test(){
  cout << "base::test()" << endl;
 }
 void test(int){
  cout << "base::test(int)" << endl;
 }
};
class derived : public base{
public:
 void test(){
  cout << "derived::test()" << endl;
 }
};

此时derived::test()会隐藏(hide)父类中的两个test重载函数(base::test()和base::test(int)),因此我们为子类中加上一个using声明:

class derived : public base{
public:
 void test(){
  cout << "derived::test()" << endl;
 }
 using base::test;//此声明放在test前面和后面效果都一样
};

现在会不会出现下面所述的情况呢?
---------------------------------------------------------------------------------------------------------------
既然using base::test将父类中的两个test函数都引入子类,则子类中就相当于有了一个void test()函数,所以我们在子类中重新定义的void test()函数将会和从父类中引入的void test()函数发生冲突,进而出现“重定义”错误。
---------------------------------------------------------------------------------------------------------------
答案是:不会!
此时,子类中重新定义的void test()函数将“顶替”从父类中引入的void test()函数。
(PS:从父类中引入的另外一个void test(int)函数则没有发生变化(仍然是父类中的函数实现)。)

类似的另外一种情况如下,此时加入了virtual:

class base{
public:
 virtual void test(){
  cout << "base::test()" << endl;
 }
 virtual void test(double){
  cout << "base::test(double)" << endl;
 }
 void test(int){
  cout << "base::test(int)" << endl;
 }
};
class derived : public base{
public:
 void test(){
  cout << "derived::test()" << endl;
 }
};

此时derived::test()虽然重写(override)了base::test(),但是同时也隐藏(hide)父类中的两个test重载函数(一个virtual函数base::test(double)和一个nonvirtual函数base::test(int))。现在,我们为子类中加上一个using声明:

class derived : public base{
public:
 void test(){
  cout << "derived::test()" << endl;
 }
 using base::test;//此声明放在test前面和后面效果都一样
};

与上面的类似,此时derived::test()“仍然重写”了父类的base::test(),并且与父类中的base::test(double)和base::test(int)[在子类的域]中形成重载集合。


最后,留一个思考题目,如下:

class base{
public:
 virtual void test(){
  cout << "base::test()" << endl;
 }
 virtual void test(double){
  cout << "base::test(double)" << endl;
 }
 void test(int){
  cout << "base::test(int)" << endl;
 }
};
class derived : public base{
public:
 void test(){
  cout << "derived::test()" << endl;
 }
 //using base::test;
};
class A : public derived{
public:
 void test(double){
  cout << "A::test(double)" << endl;
 }
};
int main(int argc, char **argv){
  base *pb = new A;
  pb->test(2.4);
	
  //derived *pd = new A; 
  //pd->test(2.4); //错误“derived::test”: 函数不接受 1 个参数
 return 0;
}

 

 

 

问题:derived中的using base::test加上与否,对程序的结果有什么影响?
答:没有影响。(关键点:名字解析是编译时期的事情,而virtual函数动态绑定是运行时期的事情。)
(PS:但是将main函数改成“derived *pd = new A; pd->test(2.4);”,则有区别了:如果将using base::test去掉,则编译失败。)

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值