1

How could I make a function only seen by the function that calls it?

define the function I want to hide as private function is not enough, as it could still be seen by other public functions in the class.

Now I use lambda expression to define anonymous function inside function. Is there any better solution?

5
  • 1
    "Is there any better solution?" If you want it being seen from the calling function only, I'd doubt so. Commented Jan 9, 2015 at 12:39
  • 4
    Why do you need to hid it from the rest of the class? Commented Jan 9, 2015 at 12:39
  • 4
    Put the "public" function in a separate translation unit (i.e. source file) and the private function in an anonymous namespace? And I do agree with Rowland, what is the use-case for this? Commented Jan 9, 2015 at 12:43
  • Just for the same reason I don't wan't variables inside my function global... As those functions were only used by one specific public function, I don't really want their declarations appear in the definition of class... Commented Jan 9, 2015 at 12:45
  • 1
    @Alaya "I don't really want their declarations appear in the definition of class." As Joachim already mentioned put it in an unnamed namespace. Commented Jan 9, 2015 at 12:53

5 Answers 5

7

Aside from using a lambda (which you've rejected), you could implement your function in its own compilation unit, and code the supporting function in an anonymous namespace within that compilation unit.

But that supporting function would be outside the class, so you'd have to pass it all the parameters it needed. That could become unwieldly though no worse than a long lambda capture list.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a function object. For example(you can compile this, even in C++03):

#include <iostream> // only for output class foo{ int bar(){return 0;} // Only foo can see this public: int operator()(){ return bar(); } }; class baz{ public: foo do_foo; }; int main(){ baz a; std::cout << a.do_foo() << std::endl; } 

the method bar is only visible by a foo. P.S.: If you need foo to access members of baz, make it a friend.

Comments

0

A simmilar approach to cassiorenan would be to use static class functions and friends. Something like this:

void Boss(); class Worker { static void Test(){ return;} friend void Boss(); }; void Boss(){ Worker::Test(); } 

Though why would you want to do this, I don't know.

3 Comments

Making functions static members doesn't hide them.
making them private, does. static is needed just to not create an object each time.
Making them private doesn't hide them from other member implementations, which is what the OP actually requested.
0

It is possible to define function inside a function without lambdas. Just define a struct that contains required function. This approach is not much better than using lambda, but at least this is straightforward and works with older compilers too.

int func() { struct { int hiddenFunc() { return 1; } } h; int a = h.hiddenFunc() + h.hiddenFunc(); return a; } 

Comments

-1

As a slight variation from cassiorenan's solution, you could use a class containing one public static function (the visible function) and one static private function that could only be called from there. To avoid creation of objects of that class, it is enough to put a private constructor.

EDIT:

Per cassiorenan's comment, I can see that OP really needs methods and not functions. In that case, I would still use a dedicated class in a anonymous namespace to ensure it is not visible from elsewhere (even if my example is single file ...) friend to the class really used. So in below example, bar is the business class that would have a method with an externally hidden implementation (here relay_method), and foo is dedicated to the hidden method called with a pointer to the real object. In real world, the whole anonymous namespace and the implementation of the hidden method should be in the implementation file bar.cpp.

That way, the real implementation function priv_func can only be called from a bar object through bar::relay_method() and foo::bar_func(bar &).

#include <iostream> class bar; namespace { class foo { private: static int priv_func(int i) { return i * i; } foo() {} public: // only useful if true functions were needed /* static int pub_func(int i, int j) { return priv_func(i) + priv_func(j); }*/ static void bar_func(bar& b); }; } class bar { int x; int x2; public: bar(int i): x(i) {} void relay_method() { foo::bar_func(*this); } friend class foo; int getX2() const { return x2; } }; void foo::bar_func(bar& b) { b.x2 = foo::priv_func(b.x); } using namespace std; int main() { /* int i = foo::pub_func(3,4); cout << i << endl; // foo::priv_func(2); error access to private member of class foo // foo f; */ bar b(2); b.relay_method(); cout << b.getX2() << endl; return 0; } 

1 Comment

He says "function", but he actually means "method". Observe that he mentions "it could still be seen by other functions in the class". The downvote is not mine, BTW.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.