Following Modern C++.
#include
using namespace std;
template
struct int2type
{
enum { value = v };
};
template
class Factory
{
private:
T* create(int2type
{
cout << "new T" << endl;
return new T;
}
T* create(int2type
{
cout << "new T(\"hi\")" << endl;
return new T("hi");
}
public:
T* create() { return create(int2type
};
int main()
{
Factory
Factory
string* s = f.create();
cout << *s << endl;
delete s;
s = f2.create();
cout << *s << endl;
delete s;
return EXIT_SUCCESS;
}
[/code]
[output]
C:\> a.exe
new T
new T(“hi”)
hi
Hit any key to close this window…
int2type class was used to convert the given parameter into type parameter. According to the type of int2type, proper method for creating T is called.