Jump to content
  • Advertisement
Sign in to follow this  
all_names_taken

Hiding a method from those who include .h file?

This topic is 3854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to hide methods declared in an .h file from those who include that .h file? I tried the code below, but got a link error when the constructor of Foo tried to use doSomething().
//FOO.CPP
#include "foo.h"
namespace {
	void doSomething() {
		std::cout << "HELLO!\n";
	}
}

//FOO.H
namespace {
	void doSomething();
}

template <class T>
class Foo {
public:
	Foo() { doSomething(); }
};




Another alternative I tried, doesn't prevent the file that includes FOO.H from using doSomething()
//FOO.H
namespace {
	inline void doSomething() {
		std::cout << "HELLO!\n";
	}
}

//FOO.H
template <class T>
class Foo {
public:
	Foo() { doSomething(); }
};




Is it at all possible? [Edited by - all_names_taken on April 2, 2008 7:52:59 AM]

Share this post


Link to post
Share on other sites
Advertisement
a .h file is responsable for giving access to private methods/functions/variables. You want it to do exactly the opposite. I don't know if it's possible (without using private methods) but it's really not supposed :)

Why do you need that behavior?

Share this post


Link to post
Share on other sites
If you don't want it to be visible to the file including it, why even put a declaration in the header file?

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
If you don't want it to be visible to the file including it, why even put a declaration in the header file?


Because a method that IS exported from foo.h (and is defined in the .h file because it's a template) needs it.

Ah well, I guess it is impossible...

Share this post


Link to post
Share on other sites
Sorry I realize I copy+pasted the wrong sample (I experimented with the other too at the same time). Ok, here's the correct version (I edited the post above too):


//FOO.CPP
#include "foo.h"
void doSomething() {
std::cout << "HELLO!\n";
}

//FOO.H
void doSomething();

template <class T>
class Foo {
public:
Foo() { doSomething(); }
};

Share this post


Link to post
Share on other sites

//FOO.H
struct FooInternal
{
void doSomethingInternal();
};

template <class T>
class Foo : private FooInternal {
public:
Foo() { FooInternal::doSomethingInternal(); }

};


//FOO.CPP
#include "foo.h"
void doSomething() { // not exported
std::cout << "HELLO!\n";
}

FooInternal::doSomethingInternal()
{
doSomething();
}

Share this post


Link to post
Share on other sites
Anonymous namespaces generate a compilation-unit specific anonymous namespace.

You cannot access them from another compilation-unit. That is what they are +for+.

Your second example should work, your OP shouldn't. Because you told it not too in your OP.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!