Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need to handle below scenario.

C++
class A()
{
   int i;
};

class B()
{
   int j;
};

class C
{
   int k;
};

class D
{
  int l;
}

class E:A
{
  MYMACRO obj
};

If class E is derived from class A, MYMACRO macro should create object of type C whereas if class E derived from class B MYMACRO macro should create object of type D.

How to achieve this via macro?

My requirement is I should not mention the type of object while declaring.The macro should check whether the current class is derived from this or that.Then it has to create the object of different types.

If there is any other solution apart from using macro please suggest me?

What I have tried:

I explored on it but Could not get solution for my scenario.
Posted
Updated 8-May-17 5:12am
v2
Comments
[no name] 8-May-17 10:52am    
I think it is a bad design and a crazy idea. I will say, even if you can solve it with macros you will not be able to maintain this in future.
Philippe Mori 8-May-17 12:40pm    
Even if you found a way to do it, you would have to declare the mapping somewhere...

1 solution

 
Share this answer
 
Comments
[no name] 8-May-17 11:19am    
typeid is a run time Thing:
"The typeid operator allows the type of an object to be determined at _run time_."

For me it Looks more, OP tries to make a crazy construction at compile/design time.
Richard MacCutchan 8-May-17 11:54am    
I am well aware of that, but it is probably the only way to go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900