0

I created a base class like so:

#include <cstdint>
#include <iterator>
#include <cstdint>
#include <vector>
#include <initializer_list>

class PSBaseObject
{
    protected:
        inline std::int32_t* size_ptr(void* Data) { return reinterpret_cast<std::int32_t*>(Data) - 1; }
        inline const std::int32_t* size_ptr(void* Data) const { return reinterpret_cast<std::int32_t*>(Data) - 1; }

    public:
        PSBaseObject() {}
        virtual ~PSBaseObject() {}
};

template<typename T>
class PSObject : public PSBaseObject
{
    protected:
        T Data;
        inline std::int32_t* size_ptr() { return reinterpret_cast<std::int32_t*>(&Data[0]) - 1; }
        inline const std::int32_t* size_ptr() const { return reinterpret_cast<std::int32_t*>(&Data[0]) - 1; }

    public:
        PSObject() { *size_ptr(&Data[0]) = 0; *(size_ptr(&Data[0]) - 1) = -1; }
        virtual ~PSObject() {}
};

Then I inherit from PSObject (not PSBaseObject) like this:

template<typename T>
class PSArray : public PSObject<std::vector<T, CustomAllocator<T>>>
{
    private:
        typedef std::vector<T, CustomAllocator<T>> underlying_type;

    public:
        typedef std::size_t                                                 size_type;
        typedef std::ptrdiff_t                                              difference_type;
        typedef T*                                                          pointer;
        typedef const T*                                                    const_pointer;
        typedef T&                                                          reference;
        typedef const T&                                                    const_reference;
        typedef T                                                           value_type;
        typedef typename underlying_type::iterator                          iterator;
        typedef typename underlying_type::iterator::const_iterator          const_iterator;
        typedef std::reverse_iterator<const_iterator>                       const_reverse_iterator;
        typedef std::reverse_iterator<iterator>                             reverse_iterator;

        explicit PSArray() : Data(CustomAllocator<T>()) { *size_ptr() = 0; }
        explicit PSArray(size_type size) : Data(size, CustomAllocator<T>()) { *size_ptr() = size - 1; }
        explicit PSArray(size_type size, const T &value) : Data(size, std::forward<decltype(value)>(value), CustomAllocator<T>()) { *size_ptr() = size - 1; }
};


int main()
{
}

And it tells me:

error: class ‘PSArray<T>’ does not have any field named ‘Data’

Why is it not seeing the "Data" field from its base class? Also is there a way to move my typedefs to the base class and have the child class still able to see them?

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • 1
    Try adding the same inside the constructor definition and not in the inisialization list. http://stackoverflow.com/questions/2290733/initialize-parents-protected-members-with-initialization-list-c – sajas Feb 18 '14 at 04:50

1 Answers1

1

You can't initialize the Data field of the base class inside a derived class; you'll need to provide a constructor taking an object of type T, and then the base class constructor can initialize Data with that value.

jia103
  • 1,116
  • 2
  • 13
  • 20