5
#define interface class

(here is more - http://www.codeproject.com/KB/cpp/CppInterfaces.aspx )

Does that make sense? Does this clarify the difference between interfaces and implementing them classes? Or it's confusing, because it's obvious that pure virtual classes are interfaces?

Do you use it? or "macros are evil"?

5
  • 15
    Macros aren't evil; macro abuse is evil.
    – user142162
    Apr 26, 2011 at 21:30
  • 4
    I don't think it buys you much and would consider it bad practice. Apr 26, 2011 at 21:36
  • 4
    Why not try their begin and end macros as well, and see what that does to the standard library? :-) Not a good idea!
    – Bo Persson
    Apr 26, 2011 at 21:41
  • 3
    The code in that article is absolutely horrible; esp. the #define EndInterface }; part. Goodbye, syntax highlighting and auto indenting.
    – Fred Foo
    Apr 26, 2011 at 21:42
  • @Helper Method I'm not goint to use it :) My boss told me, that whould the good way to define interfaces... I couldn't agree with him. I was doing fine with class keyword, and I haven't met that new convention anywhere before.
    – dantuch
    Apr 26, 2011 at 21:46

8 Answers 8

15

I'd say it makes no sense. The "solution" proposed in that article looks like a horrible mess - what's wrong with using C++ as it is? Why introduce some crappy macros when you can just define a pure abstract class when you need one and be done with it?

It looks like someone with C#/Java background was trying to find his way in C++ and got lost. It would only introduce bugs and confusion when encountered by developers actually familiar with C++.

1
  • +1 fully agree! A language should be used as is. If you feel more comfortable with the look&feel of another language, go use it! Apr 26, 2011 at 21:42
11

A macro such as this is evil, because it hides the true language behind a facade that is not easily discerned if you don't know the secret.

A better way of defining interfaces is to use a common convention such as naming them with an "I" as the first character.

2
  • +1 Though I think that naming the implementation and not the interface is much better (by putting *impl at the end of the name of the implementation...). But this probably is a matter of taste. Apr 26, 2011 at 21:38
  • @Helper, I mentioned that convention because it's very common, but that doesn't mean it's the only convention or even the best one for a particular use case. It does have the advantage of letting one concrete class implement more than one interface. Apr 26, 2011 at 21:43
11

I really don't suggest to use these tricks, I consider smartass. Macros are not evil, but if someone includes your code and uses interface as a variable name (why not?), why does he/she have to spend 1 hour debugging with gcc -E because someone decided "interface" (which is not C++) would be smarter?

I dislike that.

1
  • 3
    Note that in this case interface is a keyword in VC++, so when the day comes that you want to port your code to a MS environment, you will have this problem anyway.
    – T.E.D.
    Jan 7, 2013 at 16:36
10

C++ practitioners have come up with useful idiomatic ways of doing things. These idioms become part of the general, common dialect that other practitioners understand and easily work with.

The C++ programmer you hire off the street will know that a base class with all pure virtual methods and no implementation is an interface. They will know that public inheritance from it means its implementing that interface. They won't know the crazy macro language specified in that article.

I'd also add that idioms are especially important in C++ due to the ability to really shoot your foot off if you're not careful. In fact the generally used idioms in C++ may be C++'s most powerful feature.

5

I would never redefine or replace an existing keyword - it only leads to confusion by everyone but the author...

2
  • 2
    @Helper, maybe because interface isn't an existing keyword? Wasn't me though. Apr 26, 2011 at 21:44
  • @Mark that is why I added the replace in my original post. Apr 26, 2011 at 21:49
4

It is bad. I wouldn't use it, ever.

Conventionally: Defines in C/C++ are typically all in capital letters to make it obvious to any reader that it is a define, and to avoid name-clashes with function or variable names.

Practically: It is a very common name "interface" and this use of it will be dangerous just on that basis. But, of course, you could make the name more distinct, e.g., MY_PLATFORM_INTERFACE or something like that.

Philosophically: This is clearly a lame attempt at turning C++ into a more purist OOP language like Java or C#, and trying to somehow make the C++ syntax more familiar to newcomers from the Java/C# world. A programming language is what it is. If you want deep changes to be made, appeal to the standard committee, and be prepared to provide strong evidence. I think that such a small change to the syntax is ridiculous. I don't think anyone would not be able to switch to C++ because he can't get used to the idea that the interface keyword doesn't exist in C++.

Idiomatically: When you program in C++, you should expect C++ programmers to look at or review your code. If you introduce a thing like that that could look like an obscure compiler-specific extension of C++, you might throw a real C++ programmer off and he might spend his precious time wondering what compiler or extension is used that enables this "interface" keyword. Stick to C++ idiomatic syntax whenever possible. Otherwise, you will have to comment, next to every use of "interface", that this keyword is just a define for "class", in which case, you might as well just comment on the class declaration that this class is an interface and just use the "class" keyword as you normally would.

2

I don't get the point of doing this. How does it help make the code more readable to developers? Or even avoiding making it more obscure?

Better to use a class naming convention such as IClassName, that should be clear without obfuscating the code from developers working on it.

2

Instead of

#define interface class

Consider using the Visual C++ keyword __interface instead, and write your #define like this:

#ifndef _MSC_VER
#define __interface class
#endif

Also prefix your interface classes with the conventional "I" for additional clarity as @jonathan-wood suggested.

Using this approach, you get true compiler support for the interface pattern under Visual Studio, and everything still compiles happily cross-platform. Because of the double underscore, you also avoid the potential problem of symbol naming collision mentioned by @gd1.

1
  • Or you could just do `` and leave it at that.
    – mrr
    Jan 1, 2013 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.