Click here to Skip to main content
15,891,950 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: CSocket::Create function has thrown exception Pin
decentsmile28-Jan-10 6:04
decentsmile28-Jan-10 6:04 
QuestionDestructor Pin
Gjm18-Jul-09 3:17
Gjm18-Jul-09 3:17 
AnswerRe: Destructor Pin
Stuart Dootson18-Jul-09 3:29
professionalStuart Dootson18-Jul-09 3:29 
AnswerRe: Destructor Pin
Bram van Kampen18-Jul-09 15:22
Bram van Kampen18-Jul-09 15:22 
AnswerRe: Destructor Pin
Iain Clarke, Warrior Programmer19-Jul-09 8:16
Iain Clarke, Warrior Programmer19-Jul-09 8:16 
GeneralUninstallation of Visual C++ 8.0 ATL (x86) WinSXS MSM Pin
VictorSotnikov18-Jul-09 2:36
VictorSotnikov18-Jul-09 2:36 
QuestionCreating new object from type_info (typeid) Pin
Tomas(cz)18-Jul-09 2:13
Tomas(cz)18-Jul-09 2:13 
AnswerRe: Creating new object from type_info (typeid) Pin
Stuart Dootson18-Jul-09 2:55
professionalStuart Dootson18-Jul-09 2:55 
Tomas(cz) wrote:
if it is possible to create a new object of some type when i only have the object type_info


No, don't think so.

Tomas(cz) wrote:
I need to have a map that stores a pair and than a function that returns new object (all the objects are derived from one base class) according to the the id in the map.


You could store function pointers in the map:

#include <map>
#include <iostream>
#include <typeinfo>

struct base_class { virtual int GetID() = 0; };

struct A : public base_class { int GetID() { return 1; } };
struct B : public base_class { int GetID() { return 2; } };

typedef base_class* BasePtr;

typedef std::map<int, BasePtr (*)()> ObjectCreationMap;

template<class ObjectType>
base_class* CreateObject() { return new ObjectType(); }

base_class* GetObject(int id)
{
   static ObjectCreationMap myMap;
   if (myMap.empty())
   {
#define INSERT(ID, TYPE) myMap.insert(std::make_pair(ID, &CreateObject<TYPE>));
      INSERT(1, A)
      INSERT(2, B)
#undef INSERT
   }
   ObjectCreationMap::iterator iter = myMap.find(id);
   if( iter != myMap.end() )
   {
      return iter->second();
   }
}

int main()
{
   BasePtr x = GetObject(1);
   std::cout << x->GetID() << " " << typeid(*x).name() << std::endl;
   x = GetObject(2);
   std::cout << x->GetID() << " " << typeid(*x).name() << std::endl;
}


Compiles, links and runs correctly with g++ 4.0.

Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: Creating new object from type_info (typeid) Pin
Tomas(cz)18-Jul-09 5:44
Tomas(cz)18-Jul-09 5:44 
GeneralRe: Creating new object from type_info (typeid) Pin
Stuart Dootson18-Jul-09 7:24
professionalStuart Dootson18-Jul-09 7:24 
AnswerRe: Creating new object from type_info (typeid) Pin
Bram van Kampen18-Jul-09 15:35
Bram van Kampen18-Jul-09 15:35 
QuestionHow to save a bitmap in Visual c++ (mfc)? Pin
Sakthi.Gs :-)18-Jul-09 0:43
Sakthi.Gs :-)18-Jul-09 0:43 
AnswerRe: How to save a bitmap in Visual c++ (mfc)? Pin
Iain Clarke, Warrior Programmer18-Jul-09 1:05
Iain Clarke, Warrior Programmer18-Jul-09 1:05 
AnswerRe: How to save a bitmap in Visual c++ (mfc)? Pin
ravidborse20-Jul-09 3:17
ravidborse20-Jul-09 3:17 
QuestionCRECT height Pin
kumar sanghvi18-Jul-09 0:03
kumar sanghvi18-Jul-09 0:03 
AnswerRe: CRECT height Pin
Naveen18-Jul-09 0:11
Naveen18-Jul-09 0:11 
GeneralRe: CRECT height Pin
kumar sanghvi18-Jul-09 0:40
kumar sanghvi18-Jul-09 0:40 
GeneralRe: CRECT height Pin
Iain Clarke, Warrior Programmer18-Jul-09 1:02
Iain Clarke, Warrior Programmer18-Jul-09 1:02 
Questiondoc file processing Pin
alashara17-Jul-09 23:47
alashara17-Jul-09 23:47 
AnswerRe: doc file processing Pin
Iain Clarke, Warrior Programmer18-Jul-09 1:10
Iain Clarke, Warrior Programmer18-Jul-09 1:10 
GeneralRe: doc file processing Pin
alashara18-Jul-09 1:46
alashara18-Jul-09 1:46 
GeneralRe: doc file processing Pin
Bram van Kampen18-Jul-09 15:46
Bram van Kampen18-Jul-09 15:46 
AnswerRe: doc file processing Pin
Alain Rist18-Jul-09 22:50
Alain Rist18-Jul-09 22:50 
AnswerRe: doc file processing Pin
Randor 19-Jul-09 13:45
professional Randor 19-Jul-09 13:45 
QuestionConversion from char * to LPWSTR Pin
Pardhu_M17-Jul-09 22:27
Pardhu_M17-Jul-09 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.