Java Code:
Class NewObjectType{
static{
Factory.register("MyType", new NewObejctType);
}
//Rest of the code goes here.
}
This is fine, your class registers with factory, but how will you achieve this in C++????
How to get static blocks in C++?
Here is the solution/code for it:
#include "stdafx.h"
#include <iostream>
#include<map>
using namespace std;
class Abs{
public:
static void reg();
};
class Factory{
static std::map<int,Abs*> map;
public:
static void regsiter(int id,Abs *a)
{
cout<<"child registered"<<endl;
map.insert(make_pair(id,a));
}
};
std::map<int,Abs*> Factory::map;
class child:public Abs{
static int ret;
public:
child()
{
display();
}
void display()
{
cout<<"child"<<endl;
}
static int reg()
{
cout<<"static method called"<<endl;
Factory::regsiter(10,new child());
return 0;
}
};
int child::ret = child::reg();
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Class NewObjectType{
static{
Factory.register("MyType", new NewObejctType);
}
//Rest of the code goes here.
}
This is fine, your class registers with factory, but how will you achieve this in C++????
How to get static blocks in C++?
Here is the solution/code for it:
#include "stdafx.h"
#include <iostream>
#include<map>
using namespace std;
class Abs{
public:
static void reg();
};
class Factory{
static std::map<int,Abs*> map;
public:
static void regsiter(int id,Abs *a)
{
cout<<"child registered"<<endl;
map.insert(make_pair(id,a));
}
};
std::map<int,Abs*> Factory::map;
class child:public Abs{
static int ret;
public:
child()
{
display();
}
void display()
{
cout<<"child"<<endl;
}
static int reg()
{
cout<<"static method called"<<endl;
Factory::regsiter(10,new child());
return 0;
}
};
int child::ret = child::reg();
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}