After reading this article: Using Interfaces in C++
I have decided to use the __interface keyword with a macro that will add virtual destructors as described in the above link.
I was happy that the __interface keyword will cause the compiler to enforce interface rules, but I was disappointed when I took it for a test drive… It turns out that the __interface keyword does not enforce the rule that a method in the interface should not contain a method body.
I can of course add a macro for function methods but I don’t want to do this. Does anyone have any other suggestions?
EDIT: portability is a none issue for me because i must compile on both windows and linux so i will use the __interface keyword when i’m on windows and not on linux , that will be in order to enforce the below rules , which can’t be enforced via abstract base class:
- Can inherit from zero or more base interfaces.
- Can only contain public, pure virtual methods.
- Cannot contain data members; properties are allowed.
- Cannot inherit from a base class.
- Cannot contain constructors, destructors, or operators.
- Cannot contain static methods.
besides the destructor issue which can be workaround one can see the advantage of using this keyword in windows env of course.
__interfaceis a microsoft specific extension. It’s not standard and not portable.Also, C++ allows for pure
virtualmethod to have function body.It’s advisable to explicitly declare
virtualdestructor in the class body.