Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i want do like this
C++
class A: public ...
{
 public:
    //strObj: a class's name
    bool isInherit(string strObj)
    {
        //A inherit from strObj? 
    }
}

thanks
Posted
Comments
joshrduncan2012 20-Nov-12 9:21am    
What is your question?
Albert Holguin 20-Nov-12 14:19pm    
Curious as to why you would want to do something like this?
Sergey Alexandrovich Kryukov 20-Nov-12 19:31pm    
Right question. Why? I mean, it maybe somewhat interesting question, but answering a question without known motivation by OP is not interesting -- it may turn out to be a waste of time...
--SA
Albert Holguin 20-Nov-12 21:59pm    
Exactly... plus if you know the motivation, there may also be a better solution to meet the end goal.
Sergey Alexandrovich Kryukov 20-Nov-12 22:03pm    
That is the main reason to tell about the goals.
--SA

1 solution

If you really need to receive a string at run time (e.g., something entered by a user), you need reflection; but C++ does not have reflection. The closest thing is RTTI (run time type information); personally, I try not to use it (never found myself in a situation where the overhead is justified).

If you can know which two classes you're checking for inheritance at compile time, google static_cast.
Also, you may be interested in the LOKI code library, or BOOST; they have templates that show whether A inherits B. But those are advanced libraries. All of them are resolved at compile time, which is a good thing (zero runtime overhead).

Hope this helps,

Pablo.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Nov-12 19:29pm    
Agree, a 5.
I would also note that run time type check is usually an OOP abuse.
--SA
Argonia 22-Nov-12 11:04am    
If your set of possible base classes is not infinite you can always downcast the child class and check which is(are) the base class(es). For downcasting i recommend dynamic_cast but for sure check all kinds :)

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