Hide functionality from headers

OpenCLEngine.h
1
2
3
4
5
namespace OpenCL
{
	unsigned int n_platforms;
	inline unsigned int getNumberOfPlatforms() { return n_platforms; }
};

I want to hide n_platforms from client *.cpp files. But I want getNumberOfPlatforms() remain inline.
How can I do this?
I thought this: (but I cannot use using namespace OpenCL anymore)
1
2
3
4
5
6
7
class OpenCL
{
	OpenCL() = delete;
	static unsigned int n_platforms;
public:
	static unsigned int getNumberOfPlatforms() { return n_platforms; }
};

Any ideas?
well i am not sure, but i think you have two ways:

1-
1
2
3
4
{
         using namespace OpenCL;
         unsigned int n_platforms;
	inline unsigned int getNumberOfPlatforms() { return n_platforms; }

so you restrict it within the curly bracket.

2- using OpenCL::yourfunction;
Hmmm..... Or this!
1
2
3
4
5
namespace OpenCL
{
	//on *.cpp file: unsigned int n_platforms;
	inline unsigned int getNumberOfPlatforms() { extern unsigned int n_platforms; return n_platforms; }
};
I like chameleon's solution. That should be enough to discourage anyone from mucking around with n_platforms, although it doesn't technically do anything to restrict access to n_platforms. Still beats the heck out of trying to do something like:

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace OpenCL
{
	class info {
		static unsigned int n_platforms;

	public:
		static unsigned int platforms() { return info::n_platforms; }
		friend class info_ ;
	};

	inline unsigned int getNumberOfPlatforms() { return info::platforms(); }
};

// user code:  unsigned n = OpenCL::getNumberOfPlatforms() ; 




Implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
namespace OpenCL {
	class info_
	{
	public:
		static unsigned int& n_platforms ; 
	};
}

unsigned int OpenCL::info::n_platforms = 0;
unsigned int& OpenCL::info_::n_platforms = OpenCL::info::n_platforms ;

// use info_::n_platforms in the implementation code. 


which is the best I could figure out. =/

If it were me though, I'd probably just forgo the inline function. This isn't code that's likely to end up in a time critical loop somewhere.
Last edited on
Topic archived. No new replies allowed.