problems with headers - part 2

I had one unsuccessful thread already about this in here
http://www.cplusplus.com/forum/beginner/182848/

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(const char* 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?
Last edited on
Probably not.

What is the problem if the <string> header is included?

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;}
}


now what is someone uses it like this
1
2
3
4
5
6
7
8
9
int main(){

int x{};
{
   Node n{&x};
}

return 0;
}
Last edited on
I tried to make it work like this with forward declaration but its not working

my_string.h
1
2
3
#pragma once

class my_string;


my_string_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

#include "my_string.h"
#include <string> 

class my_string {
	std::string val; 
public:
	my_string(const char* p) : val{ p } {}

	// ...
};


main.cpp
1
2
3
4
5
6
7
8
#include "my_string.h"


int main() {

	my_string my_str{ "this class is amazing!" };

}


Error : incomplete type not allowed
Last edited on
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.

You may be interested in the following:
http://accu.org/index.php/journals/269
Tnx for your replies first of all :D

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.
Last edited on
Topic archived. No new replies allowed.