home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,339 of 33,346   
   Cassio Neri to All   
   Re: Uniquely identifying types at runtim   
   22 May 14 07:25:12   
   
   From: cassio.neri@googlemail.com   
      
   On Monday, May 19, 2014 7:48:03 PM UTC+1, fmatthew5876 wrote:   
   > As long as each type (polymorphic or not) has a unique typeid()   
   > it should solve my problem.   
   >   
   > I will be using typeid() on classes which may not be using inheritance   
   > at all.   
      
   Actually, the case where typeid is used with types that do not belong to   
   a hierarchy is the simple case where it certainly works as you want.   
   More generally, if I understand you correctly, your expectations will   
   also be met for types that belong to a non polymorphic classes.   
      
   You might have trouble with polymorphic classes. For instance,   
      
   #include    
   #include    
      
   struct base {   
       virtual ~base() {}   
   };   
      
   struct derived : base {   
   };   
      
   int main() {   
       derived d;   
       base b;   
       base& r = d;   
   	   
       assert(typeid(b) != typeid(d));    // passes   
       assert(typeid(r) == typeid(b));    // fails but will pass if is ~base()   
                                          // is not virtual.   
       assert(typeid(r) == typeid(base)); // ditto   
   }   
      
   When you apply typeid to an object of polymorphic type, the typeinfo   
   returned is the one for the most derived type. In the example above   
   (with ~base() being virtual) typeid(r) == typeid(derived) but if you   
   make ~base() non virtual then typeid(r) == typeid(base).   
      
   HTH,   
   Cassio.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca