复制构造函数:深度复制抽象类 [英] Copy constructor: deep copying an abstract class

查看:32
本文介绍了复制构造函数:深度复制抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下(简化的情况):

Suppose I have the following (simplified case):

class Color;

class IColor
{
public: 
    virtual Color getValue(const float u, const float v) const = 0;
};

class Color : public IColor
{
public:
    float r,g,b;
    Color(float ar, float ag, float ab) : r(ar), g(ag), b(ab) {}
    Color getValue(const float u, const float v) const 
    { 
        return Color(r, g, b)
    }
}

class Material
{
private:
    IColor* _color;
public:
    Material();
    Material(const Material& m);
}

现在,有什么方法可以让我在 Material 的复制构造函数中对抽象的 IColor 进行深复制吗?也就是说,我希望复制任何 m._color 的值(颜色、纹理),而不仅仅是指向 IColor 的指针.

Now, is there any way for me to do a deep copy of the abstract IColor in the copy constructor of Material? That is, I want the values of whatever m._color might be (a Color, a Texture) to be copied, not just the pointer to the IColor.

推荐答案

看看 虚构造函数习语

这篇关于复制构造函数:深度复制抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆