0
我創建了一個基類,像這樣:無法訪問基類領域
#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() {}
};
然後我從PSObject
(不PSBaseObject
)繼承這樣的:
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()
{
}
,它告訴我:
error: class ‘PSArray<T>’ does not have any field named ‘Data’
爲什麼沒有看到「Data」f從它的基類中獲得?還有一種方法可以將我的typedefs移動到基類,並讓子類仍然能夠看到它們?
嘗試在構造函數定義中添加相同內容,而不是在inisialization列表中。 http://stackoverflow.com/questions/2290733/initialize-parents-protected-members-with-initialization-list-c – sajas