function pointers

closed account (SECMoG1T)
Hi all, is it possible to store a class member functions in a container like inside a map then call them on a particular instance of the class?

Thanks in advance.
Yes, you can:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <functional>
#include <iostream>
#include <vector>

struct foo
{
    foo(int i) : x(i) {}
    void do_a() { std::cout << x << '\n'; }
    void do_b() { std::cout << "Hello\n"; }

    int x;
};

int main()
{
    std::vector<foo> data { 1, 2, 3, 4, 5};
    using foo_mem = void (foo::*)();
    std::vector<foo_mem> actions { foo::do_a, foo::do_b, foo::do_a, foo::do_b, foo::do_a};
    for(size_t i = 0; i < data.size(); ++ i)
        (data[i].*actions[i])();
}
1
Hello
3
Hello
5
closed account (SECMoG1T)
yeah thanks @minnippa that exactly what i wanted but in my case all my attempts are faulty
i have this class..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class control
	{
	  private:
		  Automator auto_maton;
		  std::map<state, void(Automator::*)()> lib;
	  public:
		  control();
		  void execute();
		  void simulate();
	};

     ///for my constructor i got this

control::control()
	  :auto_maton{}, lib/*(*/{ { state::default, Automator::get_card }, { state::login, Automator::get_pin }, 
         { state::process, Automator::transact }, { state::done, Automator::complete } }/*)*/
	  {}

  //for my usage i have this 

     void control::execute()
	 {
		 auto st=auto_maton.get_state();
		 auto_maton.lib[st]();
	 }


the class simulates a state controlled Atm machine but it fails with a whole bunch of error, i just don't know what's wrong with it av tried a couple variation but all failed

errors!

Error	1	error C2797: 'mta::control::lib': list initialization inside member initializer list or non-static data member initializer is not implemented	113	1	mta local state
Error	2	error C2039: 'lib' : is not a member of 'mta::Automator'		118	1	mta local state
Error	3	error C4716: 'reeap' : must return a value	10	1	mta local state
	4	IntelliSense: class "mta::Automator" has no member "lib"	118	15	mta local state



what should i change? thanks.


EDIT: state is a global strongly type enumeration , all the functions in in the class Automator have a common function signature void() , when a function executes successfully it changes the state of the class automator.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum class state{ default, login, entry, process,done};

	class Automator
	{
	   private:
		   state cur_state;
		   std::string card;
		   std::map<std::string, account> host;
		   void toggle_state(const state& st);
	   public:
		   Automator();
		   void get_card();
		   void get_pin();
		   void transact();
		   void complete();
		   state get_state() const;
	};
Last edited on
1) VS AFAIK still not implemented list initializaton in member init.
Rest are probably stems from first.

auto_maton.lib[st](); to execute member function through pointer, you need to provide instance, use member function selection syntax ( .* or ->* ), and use brackets: ( this->*auto_maton.lib[st] )()
closed account (SECMoG1T)
Perfect this works great

1
2
3
4
5
(this->auto_maton.*lib[st])();///so;ved 2nd error

control::control()
	  :auto_maton{}, lib({ { state::default, &Automator::get_card }, { state::login, &Automator::get_pin }, { state::process, &Automator::transact }, { state::done, &Automator::complete } }) ///solved 1st error
	  {}


Thanks Again.
Last edited on
closed account (SECMoG1T)
Ooh wait the program terminates with this error,
 
Error ! Abort have been called


sorry it was an error with my regex usage

std::regex pattern("([[:alpha:]])(\\d{3})([.- ])?(\\d{4})([.- ])?(\\d{4})");
what's wrong here i mean i wanted a pattern similar to this one
X746-9844-9847 for testing
Last edited on
closed account (SECMoG1T)
This function gives the error in the debugger, what am i doing wrong, thanks for your help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Automator::get_card()
	{ 
		std::cout << "Enter your card : ";
		std::regex pattern("([[:alpha:]])(\\d{3})([.- ])?(\\d{4})([.- ])?(\\d{4})");
		std::smatch match;

		std::string crd("");
		int count = 3;

		while (!(std::regex_match(crd, match, pattern))&&count--)
		{
			std::getline(std::cin, crd);
		}

		if (count)
		{
			card = crd;	toggle_state(state::login);
		}

		else
			toggle_state(state::default);
	}


debugger log


Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  
This is usually a result of calling a function declared with one calling convention with a 
function pointer declared with a different calling convention.
Last edited on
[.- ]This is a meaningless range. What are you intend to do?
If you want to capture either of those 3 symbols, place dash last.
Last edited on
closed account (SECMoG1T)
1
2
([.- ])?///is a grouping of options the user can either use a dash a dot or a space between
         ////the digit groupings 


i have also updated the log detail given by the debugger in my previous post, thanks
Place dash last in group.

Does it point you to exact line?
closed account (SECMoG1T)
Whooaa! what was that , it works now
std::regex pattern("([[:alpha:]])(\\d{3})([. -])?(\\d{4})([. -])?(\\d{4})");//ok

why did it work with this Place dash last in group. av never heard of anything like that?

Thank you very much.
Because if dash is placed between two other characters, it denotes a range.
Use https://regex101.com/ to check and debug your expressions.

Alternatively, you can escape dash instead.
Last edited on
closed account (SECMoG1T)
i'll laugh at myself , av never heard of that , thanks for the insight n your time too, i'll check the link .

solved.
Topic archived. No new replies allowed.