Macro: InstanceOf(object, class)

 

I remember looking for this "InstanceOf" function when I just started programming in MQL. I have created a macro some time ago to simulate this function and share it here. Enjoy it.


#define INSTANCE_OF(object, classname)    (CheckPointer(dynamic_cast<classname *>(GetPointer(object))) ? true : false)

Usage:

if (INSTANCE_OF(MyArray, CObjArray))
   Alert("Hooray!");
 
CheckPointer() does not return a bool. Your Ternary Operator ?: is wrong. Correct and simplify
#define INSTANCE_OF(object, classname)    (dynamic_cast<classname *>GetPointer(object) != NULL)
 

@William Roeder There's a ')' missing after GetPointer.

 

In case someone's interested. Here's a function to check if an object actually is a pointer:

template<typename T>
bool IsPointer(T *p) { return true; }
template<typename T>
bool IsPointer(T p) { return false; }
template<typename T>
bool IsPointer(const T &p) { return false; }

void *ptr;
Print("IsPointer(ptr): ",IsPointer(ptr));
Reason: