从未知类创建新对象 [英] Create new object from an unknown class

查看:84
本文介绍了从未知类创建新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在一个例子中说我的问题。

例如我有一个来自未知类的指针。 aPointer;

我只知道它的基类是ABaseClass类;

现在我是另一个指针:ABaseClass * bPointer;

现在我想要从aPointer的类创建一个对象到bPointer;

我可以这样做吗?

解决方案

不,没有办法进入C ++在只有一个类似对象的基类指针时创建一个相同类型的对象。



这通常是由所谓的工厂函数完成的或者上课。在实现派生类的对象的cpp文件中,您可以实现一个函数



 MyObject * MyObjectFactory()
{
返回新的MyObject;
}





并将指向此函数的指针传递给想要创建此类对象的地方。



谷歌工厂的概念,你会发现很多很好的例子。



另一种方法是在基类中实现虚函数以创建相同类型的对象。然后在每个派生类中重写该函数以创建正确类型的对象。这实际上也是一种工厂机制。


如果你想为游戏或插件制作组件基础系统

这可以帮助你

:)



  class 组件
{
public
virtual 组件* getNewInstance(){ return new 组件;}
虚拟 const char * getName(){ return 组件;}
bool isEqual (Component * _other){ return (getName()== _other-> getName()); }
}

class MyComponent1:Component
{
Component * getNewInstance();
const char * getName();
}

class MyComponent2:Component
{
Component * getNewInstance();
const char * getName();
}

组件* MyComponent1 :: getNewInstance(){返回 new MyComponent1;}
组件* MyComponent1 :: getName(){返回 MyComponent1;}
Component * MyComponent2 :: getNewInstance(){ return new MyComponent2;}
组件* MyComponent2 :: getName(){ return MyComponent2;}

组件* g_cmp [ 4 ]。
g_cmp [ 0 ] = new MyComponent1;
g_cmp [ 1 ] = new MyComponent2;
g_cmp [ 2 ] = g_cmp [ 0 ] - > getNewInstance(); // MyComponent1
g_cmp [ 3 ] = g_cmp [ 1 ] - > getNewInstance(); // MyComponent2
std :: cout<< g_cmp [ 0 ] - > getName()<< ' \ n'; // MyComponent1
std :: cout<< g_cmp [ 1 ] - > getName()<< ' \ n'; // MyComponent2
std :: cout<< g_cmp [ 0 ] - > isEqual(g_cmp [ 2 ])<< ' \ n'; // true


< blockquote class =FQ>

引用:

现在我想从aPointer的类创建一个实例到bPointer;





你到底是什么意思?



你当然可以写:

 ABaseClass * bPointer = aPointer; 





这是你要的吗?


Hi I say my problem in an example.
for example i have an pointer from an unknown class. aPointer;
I just know it''s base class is class ABaseClass;
now I another pointer : ABaseClass* bPointer;
now I want create an object from aPointer''s class to bPointer;
can I do that ?

解决方案

No, there is no way in C++ to create an object of the same type when just having a base class pointer to a similar object.

This is what is usually done by a so-called factory function or class. In the cpp file that implements your derived class''s object you could for example implement a function

MyObject* MyObjectFactory()
{
    return new MyObject;
}



and pass along a pointer to this function to places that want to create such objects.

Google the concept of Factory and you will find plenty of good examples how it is done.

Another approach is to implement a virtual function in the base class for creating objects of same type. Then override that function in each of your derived classes to create the proper type of object. This is in fact also kind of a factory mechanism.


if u want make component base system for game or plugin
this can help u
:)

class Component
{
public:
	virtual Component* getNewInstance(){ return new Component;}
	virtual const char* getName(){ return "Component";}
	bool isEqual(Component* _other){ return (getName() == _other->getName()); }
}

class MyComponent1 : Component
{
	Component* getNewInstance();
	const char* getName();
}

class MyComponent2 : Component
{
	Component* getNewInstance();
	const char* getName();
}

Component* MyComponent1::getNewInstance(){ return new MyComponent1;}
Component* MyComponent1::getName(){ return "MyComponent1";}
Component* MyComponent2::getNewInstance(){ return new MyComponent2;}
Component* MyComponent2::getName(){ return "MyComponent2";}

Component* g_cmp[4];
g_cmp[0] = new MyComponent1;
g_cmp[1] = new MyComponent2;
g_cmp[2] = g_cmp[0]->getNewInstance();	//MyComponent1
g_cmp[3] = g_cmp[1]->getNewInstance();	//MyComponent2
std::cout << g_cmp[0]->getName() << '\n';	//MyComponent1
std::cout << g_cmp[1]->getName() << '\n';	//MyComponent2
std::cout << g_cmp[0]->isEqual(g_cmp[2]) << '\n';	//true


Quote:

now I want create an Instance from aPointer''s class to bPointer;



What do you mean exactly?

You may, of course, write:

ABaseClass * bPointer = aPointer;



Is this what you are asking for?


这篇关于从未知类创建新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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