0

I have something that i can't understand.

With one basic template class:

#ifndef  DBUFFER_HPP
#define  DBUFFER_HPP

#include  <memory>

namespace  memory {

  template <template <typename T, class Alloc = std::allocator<T> > class Stock, class Unit>
  class  DBuffer {

    typedef Stock<Unit> buffer_t;

    protected:
      const std::size_t          m_sizeMax;
      std::unique_ptr<buffer_t>  m_data;
      std::unique_ptr<buffer_t>  m_backData;

    public:
      DBuffer(const std::size_t sizeMax) : m_sizeMax(sizeMax),
                                           m_data(new buffer_t()),
                                           m_backData(new buffer_t()) {}
      virtual ~DBuffer() = default;

    public:
      const buffer_t&  current() { return *m_data; }
      void  swap() { m_data.swap(m_backData); }
  };
}
#endif

I just want to inherit from it, but :

#ifndef  VIDEO_BUFFER_HPP
#define  VIDEO_BUFFER_HPP

#include  "dbuffer.hpp"
#include  <deque>

namespace  video {

  template <typename T>
  class  VideoBuffer : public memory::DBuffer<std::deque, T> {

    private:
      static const unsigned int  VIDEO_FPS_MAX = 60;

    public:
      VideoBuffer() : memory::DBuffer<std::deque, T>(VIDEO_FPS_MAX){}
      ~VideoBuffer() = default;

    private:
      void  pop_to_back() {

        m_backData->push_front(std::move(m_data->front()));
        if (m_backData->size() > m_maxSize)
          m_backData->pop_back();
        m_data->pop_front();
      }     
#endif

But the only error is a not declared on every member that i tried to call from the base class.

Maybe i have a problem because some template type is not specified?

If somebody can explain why, i'll be thanks full.

user2466514
  • 33
  • 1
  • 6
  • You cannot access `private` members from an inherited class. – πάντα ῥεῖ Nov 19 '15 at 15:00
  • Also, you are trying to use `m_maxSize` while `m_sizeMax` is declared in the base class. – Cybran Nov 19 '15 at 15:03
  • fail sorry i have forget to change that! it was protected – user2466514 Nov 19 '15 at 15:04
  • When a name is used in a way does not depend on the template parameter, it needs to be resolved without anything that does depend on the template parameter. So you always get the problem you described using members of a template base class that way. You can add `using` lines to specify what you want to use from the base class. `typedef memory::DBuffer super;` then `using super::m_sizeMax;` etc. – JSF Nov 19 '15 at 15:07
  • i know that was easy but i didn't know that, sorry and thanks! – user2466514 Nov 19 '15 at 15:21

1 Answers1

0

To resume comment:

You have to replace private: by protected: to access parent members and inform that the members are dependant name: (for example by using this->)

void  pop_to_back() {
    this->m_backData->push_front(std::move(this->m_data->front()));
    if (this->m_backData->size() > this->m_maxSize)
        this->m_backData->pop_back();
    this->m_data->pop_front();
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • You can use this? my bad i used a typedef memory::DBuffer Base because it's seems more clear. But i'm not sure that typedef in class have some scope notions – user2466514 Nov 19 '15 at 16:20
  • In my experience, that `typdef` plus several `using` within the class definition makes much more readable/maintainable code than a lot of `this->` in each member function. But that is ultimately a style choice. Anything that tells the compiler you want that name looked up as a member, gets the job done. – JSF Nov 20 '15 at 17:55