简体   繁体   English

在C ++ 11中使用关键字的用例

[英]Use cases of keyword using in C++11

I know using in C++11 behaves same as typedef . 我知道using在C ++ 11周的行为相同typedef I have this code and found different use cases: 我有这段代码,发现了不同的用例:

template<typename T, int a>
class Base
{
public:
     std::vector<T> noise_(a);
     using VectorType = std::vector<T>;
     virtual VectorType getVector() const
     {
        return noise_;
     }
protected:
     VectorType noise_;
};

template<typename T, int a> 
class Derived : public Base<T,a>
{
public:
    using Base<T,a>::noise_;
    using VectorType = typename Base<T,a>::VectorType; 
    using Base<T,a>::getVector;
};

Here, using is used in 3 different way. 在这里, using以3种不同方式使用。 What is the purpose of the following line ( noise_ is a protected member of the base class): 下一行的目的是什么( noise_是基类的受保护成员):

using Base<T,a>::noise_;

Same for: 相同于:

using Base<T,a>::getVector;

Simply put, when the base class depends on a template parameter, its scope is not inspected to resolve names. 简而言之,当基类依赖于模板参数时,将不检查其范围来解析名称。 Hence, you cannot refer to noise_ in Derived using just noise_ . 因此,您不能仅使用noise_Derivednoise_ You should either write this->noise_ , or introduce the name with using . 您应该编写this->noise_ ,或using引入名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM