将派生类转换为另一个而不更改基类

时间:2013-04-05 21:05:19

标签: c++ inheritance casting type-conversion

我有几个子类都有相同的父级。可以使用父对象中包含的一些数据构造每个子类。我想使用基础对象中包含的信息将一个孩子变成另一个孩子(不修改基础对象)。

目前,它的实现方式如下例所示:

#include <iostream>

using namespace std;

class Data {};
class base
{
public:
    base()  {}
    base(Data input) : data(input)  {}
    virtual ~base() {   cout << "Deleting :" << this->name() << endl;   }
    template<class T> static base* CastToDerrived(base* object)
    {
        T* output = new T(object->data);
        delete object;
        return output;
    }
    virtual const char* name()  {return "base";}
    Data data;
};

class derrived1 : public base
{
public:
    derrived1() {}

    derrived1(Data input): base(input){}
    ~derrived1(){cout << "Deleting :" << this->name() << endl;}
    const char* name(){return "derrived1";}
};

class derrived2 : public base
{
public:
    derrived2(){}
    derrived2(Data input): base(input){}
    ~derrived2(){cout << "Deleting :\t" << this->name() << endl;}
    const char* name(){return "derrived2";}
};

int main(int argc, char *argv[])
{
    base* object = new derrived1();
    cout << "Created :\t"<<object->name()<<endl;
    object = base::CastToDerrived<derrived2>(object);
    cout << "Casted to :\t"<<object->name()<<endl;
}

哪个输出:

Created :   derrived1
Deleting :derrived1
Deleting :base
Casted to : derrived2

然而,这需要基类被销毁并再次创建,我想避免 - 我想破坏derrived1,使用base来构造derrived2,但保持基类完整。最好的方法是什么?

(有几个派生类,基类为所有人提供了通用接口,稍后会添加一些派生类而不修改基础)。

1 个答案:

答案 0 :(得分:0)

你不能销毁派生类并保留基类,因为它是同一个实例。 我们可以使用下面的代码来证明这一点

#include <iostream>

using namespace std;

class Data {};
class base
{
public:
    base()
    {
        printf("base pointer = %08X\n", this);
    }
};

class derrived1 : public base
{
public:
    derrived1()
    {
        printf("derrived1 pointer = %08X\n", this);
    }
};

int main(int argc, char *argv[])
{
    base* object = new derrived1();
    delete object;
    return 0;
}

输出将显示基于derived1是相同的指针 另一方面,你使用一些棘手的代码来实现你想要的。 我们的想法是使用指向Data类的指针而不是变量。 您的代码将变为如下

#include <iostream>

using namespace std;

class Data {};
class base
{
public:
    base(){data = new Data}
    base(Data* input) : data(input)  {}
    virtual ~base() {   cout << "Deleting :" << this->name() << endl;   }
    template<class T> static base* CastToDerrived(base* object)
    {
        T* output = new T(object->data);
        delete object; 
        return output;
    }
    virtual const char* name()  {return "base";}
    Data* data;
};

class derrived1 : public base
{
public:
    derrived1() {}

    derrived1(Data* input): base(input){}
    ~derrived1(){cout << "Deleting :" << this->name() << endl;}
    const char* name(){return "derrived1";}
};

class derrived2 : public base
{
public:
    derrived2(){}
    derrived2(Data* input): base(input){}
    ~derrived2(){cout << "Deleting :\t" << this->name() << endl;}
    const char* name(){return "derrived2";}
};

int main(int argc, char *argv[])
{
    base* object = new derrived1();
    cout << "Created :\t"<<object->name()<<endl;
    object = base::CastToDerrived<derrived2>(object);
    cout << "Casted to :\t"<<object->name()<<endl;
}

我希望这会有所帮助。