Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to typeof in C++

How to simulate C# typeof-command behavior in C++?

C# example:

public static PluginNodeList GetPlugins (Type type)
{
 ...
}

Call:

PluginManager.GetPlugins (typeof(IPlugin))

How to implement this using C++? Maybe QT or Boost libraries provide a solution?

What about the case if you want to implement .GetPlugins(...) in a way that it loads those kinds of objects from a file (.so or .dll)?

like image 421
Velho Kerho Avatar asked Oct 08 '09 20:10

Velho Kerho


People also ask

What is typeof () in C?

The typeof keyword is a new extension to the C language. The Oracle Developer Studio C compiler accepts constructs with typeof wherever a typedef name is accepted, including the following syntactic categories: Declarations. Parameter type lists and return types in a function declarator. Type definitions.

Is typeof in the C standard?

typeof is a extension featured in many implementations of the C standard to get the type of an expression. It works similarly to sizeof , which runs the expression in an “unevaluated context” to understand the final type, and thusly produce a size.

What type is typeof?

The Data Type of typeof The typeof operator is not a variable. It is an operator. Operators ( + - * / ) do not have any data type. But, the typeof operator always returns a string (containing the type of the operand).

What is __ Auto_type?

Using __auto_type instead of typeof has two advantages: Each argument to the macro appears only once in the expansion of the macro. This prevents the size of the macro expansion growing exponentially when calls to such macros are nested inside arguments of such macros.


2 Answers

You could use a dynamic_cast to test types as shown below:

IPlugin* iPluginPtr = NULL;
iPluginPtr = dynamic_cast<IPlugin*>(somePluginPtr);

if (iPluginPtr) {
    // Cast succeeded
} else {
    // Cast failed
}
like image 196
daveg Avatar answered Oct 20 '22 01:10

daveg


This behaviour is called RTTI (Run time type information). This technique is best to be avoided, but can be beneficial in some situations.

There are two big ways to solve this. The first way is to write an interface with a pure virtual function that returns a class specific integer reference code. This code can then be used to represent a specific type. These integers could be stored in a specific enumeration.

In derived classes you can then override the method and return that class specific type. During runtime, you can then call Plugin->getType() for instance, and it'll return its specific type. You can then perform a static_cast on the pointer to get the correct pointer of the derived type back.

The second way is to either use typeid to get the classtype of the object; but this is compiler dependant. You can also try casting your pointer using dynamic_cast; dynamic_cast returns a null pointer when it's being cast into the wrong type; and a valid one when being cast in a correct type. The dynamic cast method has a bigger overhead tho than the getType method described above.

like image 33
Charles Avatar answered Oct 20 '22 01:10

Charles