Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ define interface

#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"?

like image 546
dantuch Avatar asked Apr 26 '11 21:04

dantuch


People also ask

How do you define a interface?

In general, an interface is a device or a system that unrelated entities use to interact.

Can we create an interface in C?

You implement interfaces using structs of function pointers. You can then have the interface struct embedded in your data object struct and pass the interface pointer as first parameter of every interface member function.

What is interface define with example?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".

How do you declare interface class?

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.


2 Answers

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++.

like image 116
Jakub Januszkiewicz Avatar answered Oct 11 '22 12:10

Jakub Januszkiewicz


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.

like image 41
Mark Ransom Avatar answered Oct 11 '22 12:10

Mark Ransom