I will try to explain this lot more clear this time and with really simple example.
So here are my 2 files
main.cpp
1 2 3 4 5 6 7 8 9 10 11
#include "my_string.h"
int main() {
std::string str; //I dont want this to work
my_string my_str{ "this class is amazing!" }; // I only want this class to be
// accessible in main.cpp after
// including "my_string.h"
}
my_string.h
1 2 3 4 5 6 7 8 9 10 11
#pragma once
#include <string> //kind of have to include for my class
class my_string {
std::string val; //using string here
public:
my_string(constchar* p) : val{ p } {}
// ...
};
As you see in the comments when I include my "my_string.h" I don't want those headers what I was using to implement my_string to be included along with it. (in this case string) I only want to make my_string to be accessible when I include my header. Is there a way to make it happen?
What is the problem if the <string> header is included?
There can also be other classes that I made included that are only supposed to work in background to help my main class ticking. As I see it user should only be using well polished library not playing around with implementation details of that library. If I'll want to provide those ill make them accessible in a separate header file.
for example there might be some
1 2 3 4 5 6
class Node{
int* p
public:
Node(int * pp) : p{pp}{}
~Node() {delete p;}
}
There can also be other classes that I made included that are only supposed to work in background to help my main class ticking. As I see it user should only be using well polished library not playing around with implementation details of that library.
There is a big difference between "hiding" your own internal classes and trying to hide a standard class like std::string.
The first thing you should be using is namespaces.
Secondly you'll need to keep your internal implementations as separate as possible from your public implementations. And remember sometimes it won't be possible to totally hide all of your internal implementation details.
string was chosen only for simplicity, there would normally be that Node class, that I mentioned, as protected data member (just wanted to make example simpler).
If I put it in a namespace it will still get included, i didn't use namespace only because this was very simple example. And if someone knew that there was this extra class included (Node), he will probably know in what namespace its in so there wouldn't be any problems using it.
Haven't read that article u gave me yet, maybe there is mentioned some way to hide it.