Reputation: 9533
I want something like this:
class* defineType(ofxDTangible &t) {
if(t.angle > PI) {
return STSequencer;
}
else if(t.angle > HALF_PI) {
return STSample;
}
return NULL;
}
So i want to return not an object of the class type but the type of the class itself. Is it possible?
What i want is something like this:
classType = defineType(tang);
if(classType != NULL) {
// create the object somehow?
}
It's probably bad, but i'm just wondering if it's possible.
Upvotes: 1
Views: 13137
Reputation: 45414
you could define a tag to identify the class type:
enum class tag { null, sequencer, sample };
tag defineType(ofxDTangible &t) {
if(t.angle > PI)
return tag::sequencer;
if(t.angle > HALF_PI)
return tag::sample;
return tag::null;
}
Upvotes: 0
Reputation: 7400
No, C++ does not have Reflection.
You can however, use polymorphism to solve this type of problem:
class ofxDTangible
{
public:
float angle;
};
class STbase
{
public:
virtual ~STBase(){}
};
class STSequencer : public STbase
{
public:
STSequencer(const ofxDTangible &ofx){ /*...*/ }
};
class STSample : public STbase
{
public:
STSample(const ofxDTangible &ofx){ /*...*/ }
};
shared_ptr<STbase> defineType(ofxDTangible &t)
{
if(t.angle > PI) {
return shared_ptr<STbase>(new STSequencer(t));
}
else if(t.angle > HALF_PI) {
return shared_ptr<STbase>(new STSample(t));
}
return shared_ptr<STbase>();
}
Upvotes: 2
Reputation: 56863
The closest you can get is probably std::type_info
:
const std::type_info* defineType(ofxDTangible &t) {
if(t.angle > PI) {
return &typeid(STSequencer);
}
else if(t.angle > HALF_PI) {
return &typeid(STSample);
}
return NULL;
}
although I think this is really weird and you are not describing your real problem.
Upvotes: 1