0

I've seen answers for this in other topics on Stack Overflow, but I'm not sure what to do in my case. I'm brand new to templates and this is the first template I've /ever/ created using examples found, again, from this website. Anyhow, here is the code below and the associated error:

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);
    }
};

There are more errors but that's the main one. I learn best from examples so any help would be greatly appreciated, thank you.

Phobos D'thorga
  • 439
  • 5
  • 17

2 Answers2

1

This is what I got from the warning message (copy-construct the base, too):

SecureString(SecureString const& other) throw() : std::allocator<T>(other) {}

Better leave it up do default if you don't plan to do anything useful there:

SecureString(SecureString const&) throw() = default;

And consider dumping the throw() exception specification.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Thank you, I actually added the `std::allocator(that)` yesterday but I wasn't sure why it fixed the error until now. The link is helpful too :) – Phobos D'thorga Jun 06 '16 at 07:06
1

Your copy constructor isn't copying anything - and specifically isn't copying the base class.

The simplest fix would be to omit it (and the default constructor), since the compiler-generated versions will do the right thing.

Failing that you can explicitly copy the base:

SecureString(const SecureString &other) throw() : allocator(other) {}
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64