简体   繁体   中英

Use cases of keyword using in C++11

I know using in C++11 behaves same as 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. What is the purpose of the following line ( noise_ is a protected member of the base class):

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_ . You should either write this->noise_ , or introduce the name with using .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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