The name of this C ++ template and the arguments behind it?

In my C ++ codebase company, I see many classes defined as follows:

// FooApi.h
class FooApi {
public:
    virtual void someFunction() = 0;
    virtual void someOtherFunction() = 0;
    // etc.
};

// Foo.h
class Foo : public FooApi {
public:
    virtual void someFunction();
    virtual void someOtherFunction();
};

Foois the only class that inherits from FooApiand functions that take or return pointers to objects Fooare used instead FooApi *. It seems like it is used mainly for singleton classes.

Is this the usual, named way to write C ++ code? And what's the point? I don’t see how useful it is to have a separate clean abstract class that simply defines the class interface.

Edit [0] : Sorry, just to clarify, there is only one class from FooApiand is not going to add others later.

[1]. , .

+4
4

, , , - . , "FooApi.h" / "BarApi.h" / "QuxxApi.h". , Foo, "Foo.h" ( , ). , Foo, "Foo.cpp" (, - Foo - ).

forward-declarations, , . , -. " + " "Foo.h", - FooApi.

, Foo ( ). , Firewall. .

. , ( ). , , , .

+3

, . , NewFoo / FooApi. Foo, NewFoo.

+1

, , pImpl ( " ", " " ), - , , , , , . , , , , ( /DLL ), ( dlopen() , ). , , ///...

, , . , , get-int-member, , - , .

"" ? , , "" , . ++, , , , , / / ( ). "" - - Java, , , const final.

, , , , ( ) "", , , ( !), , "" .

+1

FooApi - , (Foo).

The bottom line is that you can implement the functionality from the point of view FooApiand create several implementations that satisfy its interface and still work with your functionality. You see some advantage when you have several descendants - functionality can work with several implementations. You can implement another type of Foo or for another platform.

After rereading my answer, I don’t think I should talk about OO again.

0
source

Source: https://habr.com/ru/post/1533455/


All Articles