Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?
Posted
Updated 2-Jan-13 0:31am
v2
Comments
Jibesh 2-Jan-13 6:30am    
your questions is not clear and its confused. what do you mean creating an instance of the same class type? do you have any sample code to share?
mohammadali1375 2-Jan-13 6:32am    
I mean Object; Create new Object
Richard MacCutchan 2-Jan-13 6:41am    
Unless there is some information that you can get from the pointer then it is not possible. C++ classes do not support reflection in the same way that C# and Java do.
mohammadali1375 2-Jan-13 6:42am    
oh. so I should solve my problem with another way.
Thanks
Maximilien 2-Jan-13 7:30am    
I don't think you can.

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.
 
Share this answer
 
v2
Comments
mohammadali1375 2-Jan-13 8:34am    
I think your right. and I do that; but now I get these error :
Error 8 error LNK1120: 1 unresolved externals C:\Users\mohammadali1375\documents\visual studio 2010\Projects\MyEngine\MEngine\Debug\MEngine.exe 1

Error 7 error LNK2001: unresolved external symbol "public: virtual class MEngineComponent * __thiscall MEngineComponent::factory(void)" (?factory@MEngineComponent@@UAEPAV1@XZ) C:\Users\mohammadali1375\documents\visual studio 2010\Projects\MyEngine\MEngine\MEngine\main.obj
mohammadali1375 2-Jan-13 8:39am    
does it true ? :
class TestComponent : public MEngineComponent
{
public:
TestComponent()
{
}
virtual MEngineComponent* factory()
{
return new TestComponent();
}
};

----

class MEngineComponent
{
public:
MEngineComponent(){}
virtual MEngineComponent* factory();

};
nv3 2-Jan-13 9:06am    
You probably forgot to define the factory function for MEngineComponent as well. Either define it there, or make it abstract, e.g.

class MEngineComponent
{
...
virtual MEngineComponent: factory() = 0;
...
};
mohammadali1375 2-Jan-13 9:32am    
Oh Yea . THANKS ALOT
You've solved my big problem. ; THANKS
if u want make component base system for game or plugin
this can help u
:)

C++
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
 
Share this answer
 
Quote:
now I want create an Instance from aPointer's class to bPointer;


What do you mean exactly?

You may, of course, write:
C++
ABaseClass * bPointer = aPointer;


Is this what you are asking for?
 
Share this answer
 
Comments
mohammadali1375 2-Jan-13 6:33am    
no . I want create new object from apointer. but I dont Know whats taht class name
Jibesh 2-Jan-13 6:54am    
aPointer means your object is already created. can you write your expected code. 'create a new object from pointer' to read your mind.
CPallini 2-Jan-13 7:01am    
Technically the existence of a pointer doesn't imply the existence of a pointed object.
Jibesh 2-Jan-13 7:04am    
your are right. i was under the impression that aPointer is already created.
CPallini 2-Jan-13 7:33am    
For instance you may write
ABaseClass * bPointer = new ABaseClass(*dynamic_cast<ABaseClass*>(aPointer));
But, again, I don't know if it is what do you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900