Map of pointers to member functions in another class [duplicate] - c++

This question already has answers here:
Using generic std::function objects with member functions in one class
(6 answers)
Closed 7 years ago.
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).

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;}};
};

Related

Scope and inheritance in c++ [duplicate]

This question already has answers here:
automatically convert list of pointers to derived class to list of pointers to base class
(4 answers)
Closed 4 years ago.
I would like to know why the compiler doesn't allow the second use of "print_all" function.
I have to give an example of a bad thing that could happen if the compiler would allow it.
#include <iostream>
#include <list>
using std::list;
class foo {
class bar : public foo {
static void print_all(list<foo *> &L) {
list<foo *> LF;
list<bar *> LB;
print_all(LF); // works fine
print_all(LB); // static semantic error
}
};
};
list<foo *> is an unrelated type to list<bar *>. The function is specified to accept one, but not the other.
But class bar inherits from class foo
That is irrelevant, because the argument of your function isn't foo&. What's relevant is whether list<bar *> inherits list<foo *>. It doesn't. std::list does not have a base class.

Trying to implement polymorphism with objects [duplicate]

This question already has an answer here:
C++ Forward Declaration confusion
(1 answer)
Closed 5 years ago.
I'm trying to implement polymorphism, as can be seen by the code below. I want vector of expressions of type class expression. these expressions will have a right and left parameter object of type Parameter class. As it may have been guessed, this could recurse until one hits a string "a_string" or "an_id." However, the compiler doesn't, first of all, know what type "Expression" is for the vector. Any help is needed. Thank you.
#ifndef PARAMETER_H
#define PARAMETER_H
#include <string>
#include <vector>
using namespace std;
class Parameter
{
private:
string a_string;
string an_id;
vector<Expression> expressions;
public:
Parameter(bool expr_stringID);
};
class Expression : public Parameter
{
private:
Parameter left_parameter;
Parameter right_parameter;
char op;
public:
};
#endif
Poymorphism either requires virtual functions (at least a virtual destructor), or the CRTP to realize Static Polymorphism.
You provide neither of these with your example.

Correct syntax for array of static class method pointers [duplicate]

This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 7 years ago.
I'm new to function pointers and am getting hung up on syntax. What I'm trying to do is define, within a class, an array of functions to do string matching. The matching functions and their storing array will be static since they will be shared by all instances of the class. The functions are stored in an array so I can iterate through within match() and try different ones. Also, I'm trying to typedef the function pointer globally because similar matching functions will be used in many such classes. I've found some stuff suggesting that the signature should maybe be bool(Money::FP)(char str) but, if true, is there no way that I can define this globally (i.e. for classes other than "Money")?
The code below does not compile so please consider it as pseudocode for what I'm trying to accomplish.
Money.h:
typedef bool(*FP)(char* str);
class Money
{
private:
static FP matchers[3] = {
Money::m1,
Money::m2,
Money::m3
};
static bool m1(char* str);
static bool m2(char* str);
static bool m3(char* str);
public:
static void match(char* str);
};
It's not working because Money::m1 refers to Money type inside its declaration. Try to decouple them, eg
class Money {
private:
static FP matchers[3];
};
FP Money::matchers[3] = {
Money::m1,
Money::m2,
Money::m3
};
In any case you might consider using std::function<bool(char*)> instead that a function pointer, since you are working with C++. Performance is not an issue until you prove it to be an issue.

Access Private member function of a class [duplicate]

This question already has answers here:
How can I test private members and methods of classes?
(8 answers)
Closed 8 years ago.
I have a class A having a number of Private/Public member functions/variables. I have added a new private member function to it. As part of a testing the newly added method, I need to invoke the private member function from my test class. In the case of member variables I have seen an example like below:
#include <iostream>
using namespace std;
class test
{
private:
int myInt;
public:
int getInt () {return myInt;}
};
int main ()
{
test t;
int* p = (int*) & t;
*p = 20;
cout << t.getInt ();
}
Output:20
Is there any reliable way I can access the private member function such that there is no code modification in the class A?
You can change class access modifiers with macro #define private public for testing, but it isn't good idea. I think you need to review test approach

C++: Templates of child classes only [duplicate]

This question already has answers here:
Restrict C++ Template Parameter to Subclass
(8 answers)
Closed 7 years ago.
Is there any way to make a template only work with child classes of a base class? Something like this:
template<BaseClass T>
class FooBar
{
// ...
};
Either use a static assert from your favourite C++ library (such as this boost example), or put a call in the constructor (or other code which will always be generated when the code is used) to a do-nothing function taking a BaseClass type, for example:
template<class T>
class FooBar
{
public:
FooBar () {
Check(static_cast<T*>(0));
}
private:
void Check ( BaseClasse* ) {}
};
(not tested)
Not directly, but you can test it in the constructor using Boost:
#include <boost/type_traits/is_base_of.hpp>
#include <boost/static_assert.hpp>
template<class T>
class FooBar{
FooBar(){
BOOST_STATIC_ASSERT(boost::is_base_of<BaseClass,T>::value);
}
};
Or if you don't want a static assert, something like this is also nice sometimes:
typedef char ERROR_T_must_be_derived_from_BaseClass[boost::is_base_of<BaseClass,T>::value ? 1 : -1];
Since whoever compiles your code will be brought to this line and has a "readable" error message.
There are ways to make it work with a static assert. See Boost.StaticAssert