gpt4 book ai didi

c++ - 在不更改基类的情况下将一个派生类转换为另一个派生类

转载 作者:太空狗 更新时间:2023-10-29 23:17:57 29 4
gpt4 key购买 nike

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

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

#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,但保持基类完好无损。最好的方法是什么?

(有几个派生类,基类提供了所有的公共(public)接口(interface),一些派生类以后会添加而不修改基类)。

最佳答案

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

#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;
}

输出将显示 base 和 derived1 是同一个指针另一方面,您使用一些棘手的代码来实现您想要的。这个想法是使用指向数据类的指针而不是变量。你的代码将变成如下

#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;
}

我希望这会有所帮助。

关于c++ - 在不更改基类的情况下将一个派生类转换为另一个派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843549/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com