Click here to Skip to main content
12,556,660 members (65,677 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
i want do like this
class A: public ...
    //strObj: a class's name
    bool isInherit(string strObj)
        //A inherit from strObj? 
Posted 20-Nov-12 3:11am
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...
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.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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,

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161026.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100