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.
Foo
FooApi
FooApi *
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]. , .
, , , - . , "FooApi.h" / "BarApi.h" / "QuxxApi.h". , Foo, "Foo.h" ( , ). , Foo, "Foo.cpp" (, - Foo - ).
forward-declarations, , . , -. " + " "Foo.h", - FooApi.
, Foo ( ). , Firewall. .
. , ( ). , , , .
, . , NewFoo / FooApi. Foo, NewFoo.
NewFoo
, , pImpl ( " ", " " ), - , , , , , . , , , , ( /DLL ), ( dlopen() , ). , , ///...
dlopen()
, , . , , get-int-member, , - , .
"" ? , , "" , . ++, , , , , / / ( ). "" - - Java, , , const final.
const
final
, , , , ( ) "", , , ( !), , "" .
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.
Source: https://habr.com/ru/post/1533455/More articles:type argument is not within the scope of type variable - javaDelphi Spring General Frame Register - genericsTurn an array of hashes into an array - ruby | fooobar.comSpring webapp - закрытие потоков при остановке приложения - javaАннотационная версия ApplicationListener и аналогичные интерфейсы - javaAngularjs: sorting shows different results in Chrome and Firefox - sortingCan we create and import modules in codekulptor? - pythonDoes SecureRandom string with tr translate show an abundance of underscores? - passwordsPassword encryption algorithm throws an exception when converting to JS from PHP - javascripthow to change ios7 interactivePopGestureRecognizer scope? - ios7All Articles