我已经在Stack Overflow的其他主题中看到了这个问题的答案,但我不知道该怎么办。我是模板的新手,这是我使用的例子中创建的第一个模板,再次来自本网站。无论如何,这里是下面的代码和相关的错误:
warning: base class 'class std::allocator<char>' should be explicitly initialized in the copy constructor [-Wextra]
SecureString(const SecureString &) throw() {}
template<class T> class SecureString : public std::allocator<T> {
public:
template<class U> struct rebind { typedef SecureString<U> other; };
SecureString() throw() {}
SecureString(const SecureString &) throw() {}
template <class U> SecureString(const SecureString<U>&) throw() {}
void deallocate(T *p, size_t n) {
#ifdef _WIN32
SecureZeroMemory((void *)p, n);
#elif __linux__
std::fill_n((volatile char *)p, (n * sizeof(T)), 0);
#endif
std::allocator<T>::deallocate(p, n);
}
};
还有更多错误,但这是主要错误。我从例子中学得最好,所以非常感谢任何帮助,谢谢。
答案 0 :(得分:1)
这是我从警告信息中得到的(复制 - 构建基础):
SecureString(SecureString const& other) throw() : std::allocator<T>(other) {}
如果你不打算在那里做任何有用的事情,最好留下default
:
SecureString(SecureString const&) throw() = default;
答案 1 :(得分:1)
您的复制构造函数不会复制任何内容 - 并且特别是不复制基类。
最简单的解决方法是省略它(和默认构造函数),因为编译器生成的版本会做正确的事情。
如果你没有明确复制基础:
SecureString(const SecureString &other) throw() : allocator(other) {}