Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
i want do like this
class A: public ...
{
 public:
    //strObj: a class's name
    bool isInherit(string strObj)
    {
        //A inherit from strObj? 
    }
}
thanks
Posted 20-Nov-12 3:11am
Comments
joshrduncan2012 at 20-Nov-12 9:21am
   
What is your question?
Albert Holguin at 20-Nov-12 14:19pm
   
Curious as to why you would want to do something like this?
Sergey Alexandrovich Kryukov at 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 at 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 at 20-Nov-12 22:03pm
   
That is the main reason to tell about the goals.
--SA

1 solution

Rate this: bad
good
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,
 
Pablo.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 20-Nov-12 19:29pm
   
Agree, a 5.
I would also note that run time type check is usually an OOP abuse.
--SA
Argonia at 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
0 OriginalGriff 405
1 Sergey Alexandrovich Kryukov 170
2 CHill60 160
3 RyanDev 135
4 Richard MacCutchan 115


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