Click here to Skip to main content
Sign Up to vote bad
good
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:21
Edited 2 Jan '13 - 0:31

Comments
jibesh - 2 Jan '13 - 6:30
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:32
I mean Object; Create new Object
Richard MacCutchan - 2 Jan '13 - 6:41
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:42
oh. so I should solve my problem with another way. Thanks
Maximilien - 2 Jan '13 - 7:30
I don't think you can.

3 solutions

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  
Comments
mohammadali1375 - 2 Jan '13 - 8:34
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:39
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:06
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:32
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
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  
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 - 2 Jan '13 - 6:33
no . I want create new object from apointer. but I dont Know whats taht class name
jibesh - 2 Jan '13 - 6:54
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:01
Technically the existence of a pointer doesn't imply the existence of a pointed object.
jibesh - 2 Jan '13 - 7:04
your are right. i was under the impression that aPointer is already created.
CPallini - 2 Jan '13 - 7:33
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 216
1 Sergey Alexandrovich Kryukov 169
2 Tadit Dash 154
3 Richard MacCutchan 145
4 Santhosh G_ 125
0 Sergey Alexandrovich Kryukov 10,338
1 OriginalGriff 7,965
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


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