Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 2-Jan-13 0:21am
Edited 2-Jan-13 0:31am
v2
Comments
jibesh at 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 at 2-Jan-13 6:32am
   
I mean Object; Create new Object
Richard MacCutchan at 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 at 2-Jan-13 6:42am
   
oh. so I should solve my problem with another way.
Thanks
Maximilien at 2-Jan-13 7:30am
   
I don't think you can.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
mohammadali1375 at 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 at 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 at 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 at 2-Jan-13 9:32am
   
Oh Yea . THANKS ALOT
You've solved my big problem. ; THANKS
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

if u want make component base system for game or plugin
this can help u
Smile | :)
 
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

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  
Comments
mohammadali1375 at 2-Jan-13 6:33am
   
no . I want create new object from apointer. but I dont Know whats taht class name
jibesh at 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 at 2-Jan-13 7:01am
   
Technically the existence of a pointer doesn't imply the existence of a pointed object.
jibesh at 2-Jan-13 7:04am
   
your are right. i was under the impression that aPointer is already created.
CPallini at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 525
1 CPallini 480
2 George Jonsson 353
3 OriginalGriff 260
4 Pheonyx 218
0 OriginalGriff 4,813
1 CPallini 3,890
2 Sergey Alexandrovich Kryukov 3,274
3 George Jonsson 2,552
4 Gihan Liyanage 2,186


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100