mardi 27 novembre 2018

How can I implement an assignment operator in a class template?`

I thought my problem was answered by this, but I can't get the following to compile:

#include <string>

template<class C>
class tKeySet {
  protected:
    bool set;
    static const std::string key;
};

template<class C, typename T>
class tKeySetType : private tKeySet<C> {
  protected:
    T m_val;
};

template<class C>
class tKeySetString: private tKeySetType<C, std::string> {
  public:
    tKeySetString<C>& operator=(const std::string &str) {
        this->set = true;
        this->m_val = str;
        return *this;
    }
};

class foo : private tKeySetString<foo> { };
template<> const std::string tKeySet<foo>::key = "foo key";

int main(void) {
    foo f;

    f = std::string("foo");

    return 0;
}

How can I make the assignment operator in tKeySetString<C> work with std::string?

Aucun commentaire:

Enregistrer un commentaire