Determining Object Type in C without "instanceof"
To determine an object's exact type in C , we employ a technique similar to Java's "instanceof" keyword. This involves a combination of dynamic casting and RTTI (Run-Time Type Information).
Dynamic Casting with RTTI Enabled:
if(NewType* v = dynamic_cast<NewType*>(old)) { // old successfully casted to NewType // Perform NewType-specific operations (e.g., v->doSomething(); ) }
Note: This method requires compiler support for RTTI.
Considerations for Dynamic Casting:
Type Enumeration Approach:
switch(old->getType()) { case BOX: Box* box = static_cast<Box*>(old); // Perform Box-specific operations break; // Additional cases for other types }
Advantages:
Drawbacks:
The above is the detailed content of How Can I Determine an Object's Type in C Without Using `instanceof`?. For more information, please follow other related articles on the PHP Chinese website!