复制构造抽象类型的数据成员

时间:2019-11-05 09:20:12

标签: c++ abstract-class base-class

请考虑以下情形:

// .NETCoreApp,Version=v3.0
1/10/2019 00:00:00 with 6
2/10/2019 00:00:00 with 7

可行,但是如果class AbsBase { public: virtual double foo() = 0; }; class Concrete1 : public AbsBase { public: double foo() { return 1; } }; class Concrete2 : public AbsBase { public: double foo() { return 2; } }; struct Config { /** * Must use a pointer to `AbsBase`, since it is an abstract base class, * and the concrete instances could be of either of the derived classes. */ AbsBase* thing; void setThing(AbsBase* t) { thing = t; } }; 是传递给thing的参数的副本,我更喜欢它。即如果setThing的类型为thing(与抽象基类相对),我可以这样做:

Concrete1

但是,如果不实例化抽象类型的对象(这是非法的),我将看不到实现此目标的方法。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

您可以使用模板函数来实现所需的行为。 我建议也使用std::unique_ptr来防止出现内存问题。

class AbsBase {
public:
    virtual ~AbsBase() = default;
    virtual double foo() = 0;
};

class Concrete1 : public AbsBase {
public:
    double foo() {
        return 1;
    }
};

class Concrete2 : public AbsBase {
public:
    double foo() {
        return 2;
    }
};

struct Config {
    /**
     * Must use a pointer to `AbsBase`, since it is an abstract base class, 
     * and the concrete instances could be of either of the derived classes.
     */
    std::unique_ptr<AbsBase> thing;

    template <typename T>
    void setThing(const T& t) {
        static_assert(std::is_base_of<AbsBase, T>::value, "T must inherit AbsBase");
        static_assert(std::is_copy_constructible<T>::value, "T must be copy-constructible");
        thing.reset(new T{t});
    }

    void test()
    {
        std::cout << (thing?thing->foo():0.0) << std::endl;
    }
};

https://gcc.godbolt.org/z/fID5UM

答案 1 :(得分:1)

除了duplicate使用clone方法之外,您还可以通过模板接受具体类型。

struct Config {
    AbsBase* thing;
    template <typename T>
    void setThing(T& t) {
        thing = new T(t);
    }
};

答案 2 :(得分:1)

这里是一个示例(使用std::cout有更多了解)。不要忘记为AbsBase创建虚拟析构函数以避免内存泄漏。

#include <iostream>

class AbsBase {
public:
    virtual double foo() = 0;
    virtual ~AbsBase() {
        std::cout << "destructor AbsBase" << std::endl;
    }
};

class Concrete1 : public AbsBase {
public:
    Concrete1() {
        std::cout << "default constructor of Concrete1" << std::endl;
    }
    Concrete1(const Concrete1 & other) {
        std::cout << "copy constructor of Concrete1" << std::endl;
    }
    Concrete1 & operator=(const Concrete1 & other) {
        std::cout << "assignment operator of Concrete1" << std::endl;
    }
    virtual ~Concrete1() {
        std::cout << "destructor of Concrete1" << std::endl;
    }
    double foo() {
        return 1;
    }
};

class Concrete2 : public AbsBase {
public:
    Concrete2() {
        std::cout << "default constructor of Concrete2" << std::endl;
    }
    Concrete2(const Concrete2 & other) {
        std::cout << "copy constructor of Concrete2" << std::endl;
    }
    Concrete2 & operator=(const Concrete2 & other) {
        std::cout << "assignment operator of Concrete2" << std::endl;
    }
    virtual ~Concrete2() {
        std::cout << "destructor of Concrete2" << std::endl;
    }
    double foo() {
        return 2;
    }
};

class Config {
private:
    /**
     * Must use a pointer to `AbsBase`, since it is an abstract base class, 
     * and the concrete instances could be of either of the derived classes.
     */
    AbsBase* thing;

public:
    Config() : thing(0) {
        std::cout << "constructor of Config" << std::endl;
    }
    ~Config() {
        std::cout << "destructor of Config" << std::endl;
        if (thing) {
            std::cout << "delete thing" << std::endl;
            delete thing;
        }
    }
    template<typename T>
    void setThing(T & t) {
        std::cout << "setThing" << std::endl;
        if (thing) {
            std::cout << "delete thing" << std::endl;
            delete thing;
        }
        thing = new T (t);
    }
    AbsBase* getThing() {
        return thing;
    }
};

int main() {
    Config config;
    Concrete1 concrete1;
    Concrete2 concrete2;
    std::cout << "=================" << std::endl;
    std::cout << "save concrete1" << std::endl;
    config.setThing(concrete1);
    std::cout << config.getThing()-> foo() << std::endl;
    std::cout << "=================" << std::endl;
    std::cout << "save concrete2" << std::endl;
    config.setThing(concrete2);
    std::cout << config.getThing()-> foo() << std::endl;
    std::cout << "=================" << std::endl;
    std::cout << "destruction of all local variables" << std::endl;
    return 0;
}

输出:

constructor of Config
default constructor of Concrete1
default constructor of Concrete2
=================
save concrete1
setThing
copy constructor of Concrete1
1
=================
save concrete2
setThing
delete thing
destructor of Concrete1
destructor AbsBase
copy constructor of Concrete2
2
=================
destruction of all local variables
destructor of Concrete2
destructor AbsBase
destructor of Concrete1
destructor AbsBase
destructor of Config
delete thing
destructor of Concrete2
destructor AbsBase
相关问题