j.jerrod.taylor
j.jerrod.taylor

Reputation: 1140

Why can you inherit from interfaces in C++

I have an application that I'm porting from C++ to Java. There is a section C++ code that I find really strange.

typedef std::string ArgName;
typedef std::map< ArgName, AnyData > ArgumentMap;

class Arguments : public ArgumentMap
{
    public:
    // Very important note: When read finds a numeric/set argument,
    // it sets anyData.kind to Int. But STILL, it fills anyData.dString,
    // just in case. So if the ArgumentMap was built by Arguments::read,
    // the dString fields are all filled.
    bool read( int argc, char **argv );

    // remains is filled with the arguments not starting with '-'.
    bool read( int argc, char **argv, std::vector<const char*>& remains );

    // const if fails, erases arg if succeeds.
    bool getNumericParam( const ArgName& name, int& num );

    // sw is true if the switch is present. The function
    // returns false if the argument value is not empty.
    bool getSwitch( const ArgName& name, bool& sw );

    bool getSwitchConst( const ArgName& name, bool& sw ) const;

    // Returns true if the switch is present. Throws an error message if
    // if the argument value is not empty.
    bool getSwitchCompact( const ArgName& name );

    void checkEmptyArgs() const;

};

It looks like in there original C++ the author is making their Arguments class inherit from a Map. This makes no sense to me. Map is an interface which means you can't inherit from it, you can only implement it. Is this something that can be done in C++ that you can't do in Java?

Also, I don't understand why you would use a typedef. I read the definition from Wikipedia

typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to form complex types from more-basic machine types[1] and assign simpler names to such combinations. They are most often used when a standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another

But I don't understand why the author would do that here. Are they trying to say that they want to inherit from the class AnyData and that ArgumentMap should have a Map as one of its fields?

Upvotes: 1

Views: 288

Answers (4)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

This makes no sense to me. Map is an interface

In Java, it is. In C++, it's not even a class, it is a class template. C++ does not have a concept similar to Java's interfaces, although you can implement something similar with virtual inheritance.

As far as the collection classes are concerned, C++ solved with templates and generic programming what Java solved with interfaces and inheritance.

Instances of C++ map template are fully functioning classes that work similarly to Java's TreeMap. You can inherit them in the same way that you inherit from classes in Java, and because of multiple inheritance, you are not limited to a single class.

Also, I don't understand why you would use a typedef.

You use typedef to give classes meaningful names. In addition to shortening your typing, it makes your program more readable, and gives you additional flexibility at redefining the class behind the typedef later on.

Note: the fact that you can inherit from standard containers does not mean that you should do it. Read answers to this question for more information.

Upvotes: 6

Paweł Stawarz
Paweł Stawarz

Reputation: 4012

Map isn't an interface, it's a template. Moreover - the author doesn't derive his class from the map template as a whole, but he derives from a parametrized template. Without the code of the class, one can only guess why he's doing it, and what he wants to achieve.

Note that deriving from STL templates is a bad idea in general (with few exceptional cases) and usually it's much better to make the template a member of your class. Templates don't have virtual members and thus - there's no real way to change their behaviour when deriving (and that's the real point of inheritance in many cases).

And a bit about the typedefs: when you use a typedef like that, you make your code a bit easier to change in the future. When you (For any reason) decide that you want to implement your own string class, you only need to change the first line in this file (from typedef std::string ArgName; to typedef myNewStringClass ArgName;), instead of changing the code in all the places where ArgName occurs.

Upvotes: 2

vonbrand
vonbrand

Reputation: 11791

C++ has multiple inheritance, there are no "interfaces". You can certainly inherit from any class. And C++ has templates, something completely absent in Java. Templates allow you to make the compiler write specially tailored functions or classes.

Upvotes: 2

b2Wc0EKKOvLPn
b2Wc0EKKOvLPn

Reputation: 2074

Interfaces are a language construct specific to Java. They do not exist in C++. In C++ you only have classes. You can have abstract classes (classes with unimplemented methods), and eventually you can use them to enunciate interfaces.

Upvotes: 4

Related Questions