0

There is a function that we would like only one other function to be the sole caller of:

void bar(){

}

void foo(){

}

void baz(){

}

All three functions are located within the same class. We would like foo() to be able to call bar(), but not allow baz() to call bar().

So far, this answer seems to suggest the solution is to either have a lambda function within a function, or to create an struct / anonymous struct within a function.

Can we have functions inside functions in C++?

Is there a way we can achieve our goal, without having to move bar() within foo() ie - without having to create a lambda or struct within foo ?

This is just a technical experiment to explore whether it is possible or not - it is not about what we "should" be doing, but rather to simply see if it is possible.

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • maybe this can help you: https://stackoverflow.com/a/4324780/13687491 – nanu_nana Jun 30 '20 at 05:03
  • 1
    @nanu_nana Thanks! But that's exactly the same link I have included in my question ! :) – Rahul Iyer Jun 30 '20 at 05:04
  • 1
    No, this isn't possible any other way. And I would strongly call into question the rationale for doing this in the first place. – cdhowie Jun 30 '20 at 05:12
  • @cdhowie I highlighted in bold - we're simply trying to see what is possible in c++ or not. Don't think it deserves -1. :) – Rahul Iyer Jun 30 '20 at 05:15
  • 1
    Why do you assume that I downvoted? The answer to the question, then, is no -- unless you move the function in question into the function which is allowed to call it. There is no access modifier stricter than `private`. – cdhowie Jun 30 '20 at 05:17

2 Answers2

1

There are basically 6 areas where you can find out, which function is calling which or is called by another function.

  • Potentially by the linker
  • Definitely by the compiler
  • By the editor. A smart editor can show cross references
  • By 3rd party static code analysis tool
  • Unfortunately not by the C++ language with the code as shown
  • Yes, during runtime, with some hacks

I guess, you are refering to the C++ language.

And therefore the answer is No with the given constraints.

A M
  • 14,694
  • 5
  • 19
  • 44
1

You can return immediately from A::foo() if it is not called from A::bar() as in the following code. But I don't know how strict did you mean not callable from anything other than A::bar(). In any case, if we remove the std::cout lines, optimisation might remove the a.baz() altogether rendering A::foo() effectively not callable from A::baz().

#include <iostream>

class A;

typedef void (A::*AFunc_t)() ; // = nullptr;

class A
{
public:
   A() : Func(nullptr) {};

   void bar() {
      Func = &A::bar;

      // do stuff

      foo();
   }

   void foo() {
      if (Func != &A::bar) {
         std::cout << "foo is ***NOT*** called from bar so we return!" << "\n";
         return;
      }

      // do stuff
      std::cout << "foo is called from bar so do something!" << "\n";
   }

   void baz() {
      Func = &A::baz;

      // do stuff

      foo();

   }


   AFunc_t Func;
};



int main()
{
   A a;
   a.baz();
   a.bar();

   return 0;
}
stackoverblown
  • 734
  • 5
  • 10