1
votes

Trying to relearn C++ after a long-term dose of Java, I've read on SO and also vaguely remember that when a class is declared in a header file and defined in a cpp file, that splits its interface (the h file) from its implementation (the cpp file).

But how can the h file be considered an interface if it contains private member variable declarations? The choice of member variables constrains the implementation, does it not? A different implementation may use an entirely different set of variables.

For example, a complex number class could be implemented with real and imaginary variables, or with magnitude and argument variables, yet support the same set of getters and setters and what have you.

Wouldn't the only way of separating interface from implementation be by inheriting pure-virtuals from a base class akin to Java's interfaces?

1
You can use the PImpl idiom if it really bothers you or the separation benefit outweighs the PImpl cost. - chris
Don't consider it an interface that way. Think of it more as an interface to the compiler/linker, which need to know e.g. the size of the class, which is determined by all non-static member variables. - emlai
If that that notion of "interface" doesn't match your own, then don't use it. - juanchopanza
@zenith That makes sense. Thanks. - Museful
@juanchopanza From where I stand it seems like you are telling me to ignore what I don't understand, rather than trying to learn about it. - Museful

1 Answers

1
votes

No. Header Files and interfaces were constructed in C long before C++. A common methodology of doing this in C land is through opaque types. Where an object has a pointer to something that is defined as char** or char[x] only denoting the size for you so you can't see it. Good examples include most anything related to Sybase and IBM middleware. You can't 'construct' objects directly, you have to go through their factory methods.

In C++ this is often realized via the Pimpl pattern and private members. While you can still see some of the bits; you can still program against the interface. Furthermore not EVERY type will be exposed to you. Its not as cut and dry.

Like anything else there are trade-offs. With Interfaces ( which more accurately turns into a templated class IMHO) every single function has to be defined over and over again for every child class; this is good for run-time performance. You can also do a abstract base class composed entirely of pure virtual functions; and this is indeed in many ways an interface; except you've also taken on the cost of virtual function look-ups for everything, some people don't like this.

You could argue points both ways till the cows come home... people write exhaustive papers on this stuff.

In the end I would argue you are taking Java too literally as to what an INTERFACE is. While there often isn't a concrete thing in a header that says literally "i am an interface" its often there and can be gleaned and discovered purely from the headers.

It's also noteworthy as an interface because with the headers and shared object you can know how to 'use' the classes and compile against them without having the source code. So again, it is an interface; just not always as "pure" as the one you see in java.