0

I know, that this question was already asked here, but I believe that my particular example is unique:

#include <functional>
#include <map>
#include <vector>

class Bar{
    public:
        static unsigned myFunc(const std::vector<std::string> &input){return 1;};
};

class Foo{
    friend class Bar;
    public:
        using CommandFunction = std::function<unsigned(const std::vector<std::string> &)>;
        std::map<std::string, CommandFunction> Commands;
};

int main(){
    Foo oFoo;
    Bar oBar;
    oFoo.Commands["myFunc"] = oBar.myFunc;
    return 0;
}

I want to make the myFunc function non-static, so it will be able to access private members of the Bar class. But I have no clue how to implement this idea. Simply removing the static keyword obviously will raise an error during compilation (invalid use of non-static function). Is there any 'clean' way of solving this problem? And by 'clean' I mean not using global variables and objects.

Update

I think I need to clarify the purpose of the design I described above. I'm using a wrapper to the GNU readline library. It is represented by the Foo class. Basically it holds a set of function pointers inside the Commands map and executes them based on the user input.

The Bar class is a set of functions which share common resources (private members of the Bar class).

Tomasz Kasperczyk
  • 1,991
  • 3
  • 22
  • 43
  • "I want to make the `myFunc` function non-static, so it will be able to access private members of the `Bar` class." That makes no sense. `myFunc` is a member function, so it can already access the private members of all `Bar` objects to which it has access. – Angew is no longer proud of SO Feb 09 '16 at 13:11
  • @Angew No it can't. A static member function can only access static class members and functions. How is a static function supposed to know which object to change the variable of? – NathanOliver Feb 09 '16 at 13:12
  • 1
    Another note, regarding "but I believe that my particular example is unique." Believing is not enough. You have to explain how your case differs, by discussing the duplicate(s) and why they don't apply. – Angew is no longer proud of SO Feb 09 '16 at 13:12
  • @Angew "You cannot access a non static member inside a static method unless you explicitly make available the object instance inside the member function.(Pass object instance explicitly as argument or use a global instance which can be accessed inside the function)" – Tomasz Kasperczyk Feb 09 '16 at 13:13
  • @NathanOliver It *can* indeed access private members, if it can get hold of a `Bar` object somehow. That's what I was trying to say. – Angew is no longer proud of SO Feb 09 '16 at 13:13
  • @user3125731 Yes, exactly. It can access private members, if you give it an object whose private members it can access. – Angew is no longer proud of SO Feb 09 '16 at 13:14
  • @Angew OK. They way you said it made it sound like you can just access an objects private data. – NathanOliver Feb 09 '16 at 13:14
  • @Angew And that's exactly what I'm asking about - how can I implement the 'holding of Bar object'? – Tomasz Kasperczyk Feb 09 '16 at 13:15
  • @user3125731 Either pass it as a parameter, or bind it into the functor when creating the mapping. Something like `oFoo.Commands["myFunc"] = [&oBar](const std::vector &i) { oBar.myFunc(i); };` – Angew is no longer proud of SO Feb 09 '16 at 13:22
  • 1
    @user3125731 Did you read the answer on the duplicate? It explains how to do this. – NathanOliver Feb 09 '16 at 13:25
  • @NathanOliver I did, and I'm trying to implement it. Sorry for making a duplicate question. When I wrote about the fact that my example is 'unique', I wasn't aware of the existence of the question you linked. Next time I'll do a better research. – Tomasz Kasperczyk Feb 09 '16 at 13:38
  • please don't add solutions to your question, if you know the solution you are encouraged to answer your own question using the answer button. – Dan Beaulieu Feb 09 '16 at 14:39

1 Answers1

0

Lambda could be a solution for you:

class Bar{
    public:
        std::function<unsigned(const std::vector<std::string>&)> myFunc{ [](const std::vector<std::string>& x){return 1;}};

};
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • Well it will work, but it will break my design. The `Bar` class represents a set of functions which share common objects (connections with MySQL databases, pointers to class instances etc.). These objects are private members of the `Bar` class. The `Foo` class represents a CLI - user enters a command and it is executed based on the `Commands` map. I hope you get the idea. – Tomasz Kasperczyk Feb 09 '16 at 13:19
  • So please edit your question that this solution is not acceptable for you. So they can reopen your question. – Humam Helfawi Feb 09 '16 at 13:25
  • @HumamHelfawi What is wrong with the dupe target? It shows all the ways this can be done. – NathanOliver Feb 09 '16 at 13:27
  • I do not have any problem. But the OP do not want a std::function solution so I just told the OP to state that clearly in the question so the duplication could be removed and someone would answer with something helpful for the OP – Humam Helfawi Feb 09 '16 at 13:30
  • There is already an answer that explains how to do this without a lambda on the dupe target. There is no reason to reopen for a an answer that is specifically catered to the OP. – NathanOliver Feb 09 '16 at 13:31
  • Never mind.. may be you are right – Humam Helfawi Feb 09 '16 at 13:32