CRTP and Factories

A

alexander.stippler

Hi

I try to combine two things, which perhaps aren't compatible at all.
But I at least look for alternatives:
I want to instantiate objects where the type is chosen based on some
input parameter (string). So I thought of the Factory pattern. But the
classes I want to instantiate all use the CRTP pattern, so I can't
choose their 'normal' base class as type for the objects created by the
factory, since for that I would need to know the exact type, and that
is 'hidden' in the string. I hope I expressed the problem
understandable.
Are their any ways to get that functionality?

regards,
Alex
 
R

Raider

I try to combine two things, which perhaps aren't compatible at all.
But I at least look for alternatives:
I want to instantiate objects where the type is chosen based on some
input parameter (string). So I thought of the Factory pattern. But the
classes I want to instantiate all use the CRTP pattern, so I can't
choose their 'normal' base class as type for the objects created by the
factory, since for that I would need to know the exact type, and that
is 'hidden' in the string. I hope I expressed the problem
understandable.
Are their any ways to get that functionality?

Your factory can return the structure like this:

struct ObjectCreateInfo
{
void* Object; // pointer to the created object
enum { ... } ObjectType; // type of the object created (enum or just
std::type_info)

// optional
typedef void Deleter(void*);
Deleter* ObjectDeleter; // function to call do delete object instance
}

So, your factory will be like this:

class YourFactory
{
public:
ObjectCreateInfo CreateObjectFormString(const string&);
}
 
A

alexander.stippler

Raider said:
Your factory can return the structure like this:

struct ObjectCreateInfo
{
void* Object; // pointer to the created object
enum { ... } ObjectType; // type of the object created (enum or just
std::type_info)

// optional
typedef void Deleter(void*);
Deleter* ObjectDeleter; // function to call do delete object instance
}

So, your factory will be like this:

class YourFactory
{
public:
ObjectCreateInfo CreateObjectFormString(const string&);
}

I do not understand how this solves my problem. How do I call a method
defined for the object type to be created from the string afterwards?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top