1

I'm reading a C++ file. There is some structure I don't understand in C++, but I don't know how to google to learn this. Here is a piece of code that i'm reading :

template <typename value_type>
class iterstack: private vector<value_type> {
   private:
      using vector<value_type>::crbegin;
      using vector<value_type>::crend;
      using vector<value_type>::push_back;
      using vector<value_type>::pop_back;
      using vector<value_type>::back;
      typedef typename vector<value_type>::const_reverse_iterator
              const_iterator;
   public:
      using vector<value_type>::clear;
      using vector<value_type>::empty;
      using vector<value_type>::size;
      const_iterator begin() { return crbegin(); }
      const_iterator end() { return crend(); }
      void push (const value_type& value) { push_back (value); }
      void pop() { pop_back(); }
      const value_type& top() const { return back(); }
};

In above code, theresomething I don't understand.

Firstly :

class iterstack: private vector<value_type>

what is the difference above line with class iterstack: vector<value_type> (without private keyword)

Secondly :

using vector<value_type>::crbegin;

I'm really don't understand this line. using keyword, I often use under this form : using namespace std Another way of using makes me confusing.

Please tell me the meaning of two points above.

Thanks :)

Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107

5 Answers5

3

The private in class iterstack: private vector<value_type> means that vector<value_type> is a private base class. Base classes of a class are private by default so in this case the added private keyword has no effect. If a base class is private then its public and protected interface is added as private functions etc. to the derived class.

class Base {
    public:
    void fun(){}
};
class Derived1 : public Base { };  //fun() is public member function
class Derived2 : Base{}; //fun() is private
class Derived3 : private Base {}; //fun is private
class Derived4 : private Base {
    public:
    using Base::fun; //fun is public
}

With using vector<value_type>::crbegin; the public member function crbegin which is private in iterstack (because it is from a base class that is inherited with private keyword) is made public.

odinthenerd
  • 5,422
  • 1
  • 32
  • 61
  • The base class' *non-private* members are accessible as private members of the derived type. This means *protected* are also accessible. – juanchopanza Apr 19 '14 at 07:55
  • No, `crbegin` isn't made public; that using-declaration is private. (Just like your last example, which incorrectly says "fun is public"). `clear`, `empty` and `size` are made public. – Mike Seymour Apr 19 '14 at 09:05
  • thanks for the corrections juanchopanza and Mike Seymour, I must not be awake yet ;) – odinthenerd Apr 19 '14 at 09:16
2

You're simply inheriting privately from the vector. In which the base class is a vector and derived class is an iterstack.

And next thing you're doing is making crbegin public in the iterstack.

Fahad Siddiqui
  • 1,829
  • 1
  • 19
  • 41
2

what is the difference above line with class iterstack: vector<value_type> (without private keyword)

There's no difference. In a class defined using the class keyword (rather than struct), inheritance is private by default; so it is private whether or not you explicitly say so (as the code does) or leave the private keyword out.

I'm really don't understand this line. using keyword

That's a using declaration, which makes a single name available in the current scope. It's used here to make names from the (private) base class available as members of this class.

I often use under this form : using namespace std

That's a using directive which dumps the entire contents of a namespace into the current scope. If you often use using namespace std to dump the standard library into the global namespace, then that's a bad habit that you should get out of; it pollutes the namespace with many, many unwanted names, which could clash with names your program wants to use. Namespaces exist for a good reason - use them.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Your first question is about private inheritance. Inheriting with the private keyword, as in class iterstack: private vector<value_type>, essentially makes the fact that the class is inheriting from vector<value_type> known only to iterstack. That is, it inherits from vector<value_type>, and can use all of vector<value_type>'s functionality internally, but the rest of the program doesn't know that it inherits from vector<value_type> and so can't treat it as an object of type vector<value_type>, as it can in class iterstack: public vector<value_type>. You can find more info about private inheritance here: Difference between private, public, and protected inheritance

Your second question is about the usage of the using keyword in using vector<value_type>::crbegin;. The word using in this context has the effect of making crbegin from vector<value_type> a public member of the class. It's essentially doing the same thing as using namespace std;, except instead of introducing all of the names from the std namespace into the namespace of a function, it's introducing the specific name crbegin from vector<value_type> into the namespace of the class, which has the effect of making it a public member of the class. You can find more info about the many uses of the using declaration here: http://en.cppreference.com/w/cpp/language/using_declaration

Community
  • 1
  • 1
Rose Kunkel
  • 3,102
  • 2
  • 27
  • 53
0

the private keyword is declaring the superclass vector as private so that you cannot change the superclass.