#Include directive limit

Let's say I have a header file with a class that uses std::string.

#include <string>

class Foo
{
     std::string Bar;

     public:

     // ...
}

The user of this header file may not want to std::stringbe included in his / her project. So, how to limit the inclusion of only the title?

+2
source share
4 answers

The user of your class must include it <string>, otherwise their compiler will not know how large the object is Foo(and if constructors / destructors are Foodefined in the string, then the compiler will also not know what constructor / destructor to call the element string).

++ ( C). , , , PIMPL.

+11

, . , , .

, pimpl idiom.

, , Foo , <string> ? std (, , <string> C, , C ++ Standard Library).

+5

, , ++. , , "std::string", , , . .h.

, , - "using":

//This is how users can use an adt with same name but in different namespaces

    using std::string;
    string bar = "there";


    using my_own_lib::string;
    string bar1 = "here";
+1

You cannot do this. If the user does not want to include std::string, then he or she should not use this class at all. std::stringmust be included in the project to properly link your class.

0
source

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


All Articles