如何使用模板子类覆盖基类?

huangapple go评论58阅读模式
英文:

how to override base class with template child?

问题

以下是您要翻译的代码部分:

我有以下代码。
```c++
#include <string>
#include <map>
#include <utility>
#include <vector>
#include <cmath>

class Configuration {
protected:
    static std::map<std::string, Configuration*> commands;
    static std::vector<Configuration*> commidx;

    const std::string name;
    const int index;
    int (*snippet)(float);

public:
    Configuration(std::string name, const int index, int (*snippet)(float))
            : name(std::move(name)), index(index), snippet(snippet) {}
    virtual ~Configuration() = default;
    virtual void set(float val) {}          //should we raise an error?
    virtual float get() { return NAN; }     //should we raise an error?
};

template<typename T>
class Config : public Configuration {
    const std::string type;
    const std::string def;
    T cur, min, max;

public:
    explicit Config(const char *name, int index, const char *_type, const char *def, int (*snippet)(float), T min , T max)
    : Configuration(name, index, snippet)
    , type(_type)
    , def(def)
    , min(min)
    , max(max)
    {
        if (type == "float") {
            cur = std::stof(def);
        } else if (type == "integer") {
            cur = std::stoi(def);
        } else if (type == "bool") {
            cur = std::stof(def) != 0;
        } else {
            SPDLOG_ERROR("unknownt type {}", type);
        }
    }

    void set(T val) override {
        if (val < min)   val = min;
        if (val > max)   val = max;
        if (val != cur) {
            val = cur;
            snippet(val);
        }
    }


    T get() override {
        return cur;
    }
};

std::vector<Configuration*> Configuration::commidx {
        new Config<float>("fff", 0, "float", "0.4", nullptr, 0, 1),
        new Config<bool>("bbb", 1, "bool", "0", nullptr, 0, 1),
        new Config<integer>("iii", 8, "int", "0", nullptr, 0, 3),
};

这段代码之所以在编译时出错,是因为set()get()方法没有真正重写基类的方法。

您如何实现所期望的结果:将略有不同的模板类的指针放入同一个向量中?

英文:

I have the following code.

#include <string>
#include <map>
#include <utility>
#include <vector>
#include <cmath>

class Configuration {
protected:
    static std::map<std::string, Configuration*> commands;
    static std::vector<Configuration*> commidx;

    const std::string name;
    const int index;
    int (*snippet)(float);

public:
    Configuration(std::string name, const int index, int (*snippet)(float))
            : name(std::move(name)), index(index), snippet(snippet) {}
    virtual ~Configuration() = default;
    virtual void set(float val) {}          //should we raise an error?
    virtual float get() { return NAN; }     //should we raise an error?
};

template<typename T>
class Config : public Configuration {
    const std::string type;
    const std::string def;
    T cur, min, max;

public:
    explicit Config(const char *name, int index, const char *_type, const char *def, int (*snippet)(float), T min , T max)
    : Configuration(name, index, snippet)
    , type(_type)
    , def(def)
    , min(min)
    , max(max)
    {
        if (type == "float") {
            cur = std::stof(def);
        } else if (type == "integer") {
            cur = std::stoi(def);
        } else if (type == "bool") {
            cur = std::stof(def) != 0;
        } else {
            SPDLOG_ERROR("unknownt type {}", type);
        }
    }

    void set(T val) override {
        if (val < min)   val = min;
        if (val > max)   val = max;
        if (val != cur) {
            val = cur;
            snippet(val);
        }
    }


    T get() override {
        return cur;
    }
};

std::vector<Configuration*> Configuration::commidx {
        new Config<float>("fff", 0, "float", "0.4", nullptr, 0, 1),
        new Config<bool>("bbb", 1, "bool", "0", nullptr, 0, 1),
        new Config<integer>("iii", 8, "int", "0", nullptr, 0, 3),
};

This bombs in compilation because set() and get() methods don't really override base class.

How can I achieve desired result: put in the same vector of pointers to slightly different template classes?

答案1

得分: 1

以下是您要的代码的翻译部分:

#include <string>
#include <map>
#include <utility>
#include <vector>
#include <cmath>

class Configuration
{
protected:
    static std::map<std::string, Configuration *> commands;
    static std::vector<Configuration *> commidx;

    const std::string name;
    const int index;
    int (*snippet)(float);

public:
    Configuration(std::string name, const int index, int (*snippet)(float))
        : name(std::move(name)), index(index), snippet(snippet) {}
    virtual ~Configuration() = default;
};

template <typename T>
class ConfigurationBase : public Configuration
{

public:
    ConfigurationBase(std::string name, const int index, int (*snippet)(float))
        : Configuration(name, index, snippet)
    {
    }
    //virtual void set(T val) {}
    //virtual T get() { return NAN; }

    virtual void set(T val) = 0;
    virtual T get() = 0;
};

template <typename T>
class Config : public ConfigurationBase<T>
{
    const std::string type;
    const std::string def;
    T cur, min, max;

public:
    explicit Config(const char *name, int index, const char *_type, const char *def, int (*snippet)(float), T min, T max)
        : ConfigurationBase<T>(name, index, snippet), type(_type), def(def), min(min), max(max)
    {
        if (type == "float")
        {
            cur = std::stof(def);
        }
        else if (type == "integer")
        {
            cur = std::stoi(def);
        }
        else if (type == "bool")
        {
            cur = std::stof(def) != 0;
        }
        else
        {
            // SPDLOG_ERROR("unknownt type {}", type);
        }
    }

    void set(T val) override
    {
        if (val < min)
            val = min;
        if (val > max)
            val = max;
        if (val != cur)
        {
            val = cur;
            // snippet(val);
        }
    }

    T get() override
    {
        return cur;
    }
};

std::vector<Configuration *> Configuration::commidx{
    new Config<float>("fff", 0, "float", "0.4", nullptr, 0, 1),
    new Config<bool>("bbb", 1, "bool", "0", nullptr, 0, 1),
    new Config<int>("iii", 8, "int", "0", nullptr, 0, 3),
};
英文:

You can try something like following.

#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
class Configuration
{
protected:
static std::map&lt;std::string, Configuration *&gt; commands;
static std::vector&lt;Configuration *&gt; commidx;
const std::string name;
const int index;
int (*snippet)(float);
public:
Configuration(std::string name, const int index, int (*snippet)(float))
: name(std::move(name)), index(index), snippet(snippet) {}
virtual ~Configuration() = default;
};
template &lt;typename T&gt;
class ConfigurationBase : public Configuration
{
public:
ConfigurationBase(std::string name, const int index, int (*snippet)(float))
: Configuration(name, index, snippet)
{
}
//virtual void set(T val) {}
//virtual T get() { return NAN; }
virtual void set(T val) = 0;
virtual T get() = 0;
};
template &lt;typename T&gt;
class Config : public ConfigurationBase&lt;T&gt;
{
const std::string type;
const std::string def;
T cur, min, max;
public:
explicit Config(const char *name, int index, const char *_type, const char *def, int (*snippet)(float), T min, T max)
: ConfigurationBase&lt;T&gt;(name, index, snippet), type(_type), def(def), min(min), max(max)
{
if (type == &quot;float&quot;)
{
cur = std::stof(def);
}
else if (type == &quot;integer&quot;)
{
cur = std::stoi(def);
}
else if (type == &quot;bool&quot;)
{
cur = std::stof(def) != 0;
}
else
{
// SPDLOG_ERROR(&quot;unknownt type {}&quot;, type);
}
}
void set(T val) override
{
if (val &lt; min)
val = min;
if (val &gt; max)
val = max;
if (val != cur)
{
val = cur;
// snippet(val);
}
}
T get() override
{
return cur;
}
};
std::vector&lt;Configuration *&gt; Configuration::commidx{
new Config&lt;float&gt;(&quot;fff&quot;, 0, &quot;float&quot;, &quot;0.4&quot;, nullptr, 0, 1),
new Config&lt;bool&gt;(&quot;bbb&quot;, 1, &quot;bool&quot;, &quot;0&quot;, nullptr, 0, 1),
new Config&lt;int&gt;(&quot;iii&quot;, 8, &quot;int&quot;, &quot;0&quot;, nullptr, 0, 3),
};

huangapple
  • 本文由 发表于 2023年2月14日 08:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定