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

问题描述 投票:0回答:3

请考虑以下情形:

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(与抽象基类相对),我可以这样做:

Concrete1 thing;
void setThing(const Concrete1& t) {
    thing = Concrete1(t);
}

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

c++ abstract-class base-class
3个回答
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
投票

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

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

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
© www.soinside.com 2019 - 2024. All rights reserved.