Passing member function to member object into template parameter - c++

I am trying to create a class/struct that can take in a struct/class of different types that all have the function update().
I want to get the update() function and then put it in a vector as a pointer and then call it later, but I'm having trouble putting member function pointers into a vector, but I am having no problem putting 'classless' function pointers into the vector.
How can I put the member function pointers into the vector?
Here is my code
#include <vector>
#include <iostream>
#include <stdio.h>
using namespace std;
struct typeA
{
public:
int data = 0;
void update()
{
cout << "hello, my data is " << data << endl;
}
};
struct typeB
{
float data = 0;
void update()
{
cout << "hi, my data is " << data << endl;
}
};
class typeListTwo
{
typedef void(*updaterFunc)();
vector <updaterFunc> items;
public:
typeListTwo()
{
}
~typeListTwo()
{
items.~vector();
}
void addItem(updaterFunc newItem)
{
items.push_back(newItem); //This works
}
void doWork()
{
for (unsigned int funcIndex = 0; funcIndex < items.size(); funcIndex++)
{
items[funcIndex]();
}
}
};
class typeList
{
typedef void(*updaterFunc)();
vector <updaterFunc> items;
public:
typeList()
{
}
~typeList()
{
items.~vector();
}
template <class Item>
void addItem(Item newItem)
{
items.push_back(newItem.update); //But this does not?
//newItem.update(); //This also works by itself
}
void doWork()
{
for (unsigned int funcIndex = 0; funcIndex < items.size(); funcIndex++)
{
items[funcIndex]();
}
}
};
void aFunc()
{
cout << "123 hello" << endl;
}
void bFunc()
{
cout << "456 goodbye" << endl;
}
int main()
{
typeA aThing;
typeB bThing;
typeList listThings;
typeListTwo listThingsTwo;
aThing.data = 128;
bThing.data = -3.234;
listThings.addItem(aThing);
listThings.addItem(bThing);
listThings.doWork();
listThingsTwo.addItem(aFunc);
listThingsTwo.addItem(bFunc);
listThingsTwo.doWork();
return 0;
}

The way to go is to use std::function or use interface:
class typeList
{
std::vector<std::function<void()>> items;
public:
template <class Item>
void addItem(Item& item)
{
// Care: life time of item should be bigger than this instance
items.push_back([&](){ item.update(); });
}
void doWork()
{
for (auto& f : items)
{
f();
}
}
};

Related

How to handle polymorphism with vectors of a class the right way in C++?

I'm currently implementing an entity component system and working on a way to store vectors of components, each vector of one component type should be contiguous, this is why I'm using vector<Component>* and not vector<Component*>. I'm new to C++ and coming from Java, so I'm still struggling with some concepts regarding memory layout and polymorphism.
I've fully implemented a (seemingly) working version of this. I have a vector for each type of component there is (e.g. PositionComponent, GravityComponent, etc.), because components differ in size. But because the amount of types isn't known in compile time, I need a vector of pointers to point to these ComponentLists of different types. I'm using reinterpret_casts with a map which keeps track of the component types to accomplish this, but there has to be a less undefined way of doing this. (Hopefully!)
Here is a picture of what I want to achieve:
Component List Layout
Edit: I just notices that my program crashes with a segmentation fault after the return 0 in the main function. The call stack seems to be corrupted, as it jumps to some random address instead of actually returning. So my system is definately flawed.
Main function:
int main(int argc, char** argv)
{
EntityComponentSystem ecs;
// Fill with some components
for(int i = 0; i < 5; ++i)
{
ecs.addComponent(AComponent());
ecs.addComponent(BComponent());
ecs.addComponent(CComponent());
}
// Print all components
std::cout << "Components: " << std::endl;
// Print AComponents
std::cout << "A Components: " << std::endl;
ComponentList<AComponent>* aComps = ecs.getComponentList<AComponent>();
aComps->print();
// Print BComponents
std::cout << std::endl << "B Components: " << std::endl;
ComponentList<BComponent>* bComps = ecs.getComponentList<BComponent>();
bComps->print();
// Print CComponents
std::cout << std::endl << "C Components: " << std::endl;
ComponentList<CComponent>* cComps = ecs.getComponentList<CComponent>();
cComps->print();
return 0;
}
EntityComponentSystem class:
class EntityComponentSystem
{
private:
// To shorten some things up
typedef std::vector<ComponentList<Component>*>::iterator ComponentListIterator;
typedef std::unordered_map<std::type_index, Component::TypeID>::iterator MapIterator;
std::vector<ComponentList<Component>*> comps; // Indexed by TypeID
std::unordered_map<std::type_index, Component::TypeID> componentMap; // Maps typeid(Component) to TypeID
public:
EntityComponentSystem() : comps(), componentMap() {}
~EntityComponentSystem()
{
for(ComponentListIterator it = comps.begin(); it < comps.end(); ++it)
{
delete (*it);
}
}
template<typename T>
void addComponent(const T& component)
{
static_assert(std::is_base_of<Component, T>::value, "T must be of type Component!");
MapIterator found = componentMap.find(typeid(T));
ComponentList<T>* componentList = nullptr;
if(found == componentMap.end()) // This component derivative hasn't been added yet, so add it
{
componentList = new ComponentList<T>();
int componentId = comps.size();
this->comps.push_back(reinterpret_cast<ComponentList<Component>*>(componentList));
this->componentMap[typeid(T)] = componentId;
}
else
{
componentList = reinterpret_cast<ComponentList<T>*>(comps[found->second]);
}
componentList->push(component);
}
template<typename T>
ComponentList<T>* getComponentList()
{
Component::TypeID id = getComponentTypeId<T>();
if(id == Component::TYPE_ID_UNKNOWN) return nullptr;
return reinterpret_cast<ComponentList<T>*>(comps[id]);
}
template<typename T>
Component::TypeID getComponentTypeId()
{
static_assert(std::is_base_of<Component, T>::value, "T must be of type Component!");
MapIterator found = componentMap.find(typeid(T));
if(found == componentMap.end())
{
return Component::TYPE_ID_UNKNOWN;
}
return found->second;
}
};
ComponentList class:
template<typename T>
class ComponentList
{
private:
std::vector<T> components;
public:
void push(const T& t)
{
this->components.push_back(t);
}
void print()
{
for(typename std::vector<T>::iterator it = components.begin(); it != components.end(); ++it)
{
it->test();
}
}
};
Base Component class and its derivatives:
class Component
{
public:
typedef uint16_t TypeID;
static const TypeID TYPE_ID_UNKNOWN = -1;
virtual ~Component() = default;
virtual void test()
{
std::cout << "I'm the base!" << std::endl;
}
};
class AComponent : public Component
{
private:
static int nextValue;
int someValue; // To test if different sizes of Component derivatives cause any problems!
public:
AComponent()
{
this->someValue = nextValue++;
}
void test()
{
std::cout << "I'm AComponent! Value: " << someValue << std::endl;
}
};
int AComponent::nextValue = 0;
class BComponent : public Component
{
public:
void test()
{
std::cout << "I'm BComponent!" << std::endl;
}
};
class CComponent : public Component
{
public:
void test()
{
std::cout << "I'm CComponent!" << std::endl;
}
};

Make a collection of an abstract class type, Abstract Class vector of shared_ptr

Error
e/c++/v1/algorithm:642:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:321:9: error:
field type 'Space' is an abstract class
_T2 second;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:624:16: note:
Question
How can I define a std::vector of type Space which is an abstract class and then fill this vector with instances of the derived classes Empty, Snake, Ladder.
Context
I know abstract classes in C++ can not be instantiated. Instead I've read in several posts on this and other sites that you can create a collection of an abstract type if it the type is defined as a star * pointer or any of the <memory> managed pointer data types like std::unqiue_ptr<T>. I've tried to used shared_ptr<Space> in my case, but still unable to define the collection properly. I am compiled my code using g++ -std=c++17 main.cpp && ./a.out.
Code
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <typeinfo>
#include <queue>
#include <string>
#include <vector>
class Player
{
private:
int m_current_space = 1;
public:
Player() {}
void role_dice() {
m_current_space += floor( (rand()%10 + 1) / 3 );
}
int const get_current_space() {
return m_current_space;
}
void set_current_space(int current_space) {
m_current_space = current_space;
}
};
class Space
{
protected:
int m_id;
std::vector<Space> m_paths;
public:
Space() {} // requied to use [] operator in map
Space(int id) : m_id(id) {}
void add_path(Space& s) {
m_paths.push_back(s);
}
int get_id() {
return m_id;
}
virtual std::string class_type() = 0;
};
class Empty : public Space
{
public:
Empty(int id) : Space(id) {}
std::string class_type() {
return "Empty";
}
};
class Ladder : public Space
{
public:
Ladder(int id) : Space(id) {}
virtual void event(Player& p) {
p.set_current_space(1);
}
std::string class_type() {
return "Ladder";
}
};
class Snake : public Space
{
public:
Snake(int id) : Space(id) {}
virtual void event(Player& p) {
p.set_current_space(4);
}
std::string class_type() {
return "Snake";
}
};
class Board
{
private:
std::map<int, Space> m_board;
public:
void add_space(Space& s) {
m_board[s.get_id()] = s;
}
void draw_board() {
int i = 1;
for(auto const& [space_key, space] : m_board) {
if(i%3 == 0) {
std::cout << "○\n";
}
else if(typeid(space) == typeid(Snake)) {
std::cout << "○-";
}
else {
std::cout << "○ ";
}
++i;
}
}
void update_player_on_board(int position) {
int i = 1;
for(auto const& [space_key, space] : m_board) {
if(i%3 == 0) {
if (space_key == position) {
std::cout << "●\n";
}
else {
std::cout << "○\n";
}
}
else if(typeid(space) == typeid(Snake)) {
std::cout << "○-";
}
else {
if (space_key == position) {
std::cout << "● ";
}
else {
std::cout << "○ ";
}
}
++i;
}
}
const std::map<int, Space> get_board() {
return m_board;
}
friend std::ostream &operator<<(std::ostream& os, const Board& b) {
return os;
}
};
class GameStateManager
{
private:
std::string m_state = "game over";
bool m_playing = false;
public:
std::string const get_state() {
return m_state;
}
void set_state(std::string state) {
m_state = state;
}
};
int main()
{
std::cout << "Welcome to Bowser's 9 board game\n";
std::cout << "Start? y(yes) n(no)\n";
GameStateManager game_manager;
game_manager.set_state("playing");
auto space1 = std::make_shared<Space>(1);
auto space2 = std::make_shared<Space>(2);
auto space3 = std::make_shared<Space>(3);
auto space4 = std::make_shared<Space>(4);
auto space5 = std::make_shared<Space>(5);
auto space6 = std::make_shared<Space>(6);
auto space7 = std::make_shared<Space>(7);
auto space8 = std::make_shared<Space>(8);
auto space9 = std::make_shared<Space>(9);
std::vector<std::shared_ptr<Space>> v {
space1, space2, space3,
space4, space5, space6,
space7, space8, space9
};
Board bowsers_bigbad_laddersnake;
for(int i = 0; i < 10; ++i) {
bowsers_bigbad_laddersnake.add_space(*(v[i]));
}
bowsers_bigbad_laddersnake.draw_board();
Player mario;
int turn = 0;
while(game_manager.get_state() == "playing") {
std::cin.get();
std::cout << "-- Turn " << ++turn << " --" << '\n';
mario.role_dice();
bowsers_bigbad_laddersnake.update_player_on_board(mario.get_current_space());
if (mario.get_current_space() >= 9) {
game_manager.set_state("game over");
}
}
std::cout << "Thanks a so much for to playing!\nPress any key to continue . . .\n";
std::cin.get();
return 0;
}
You seem to have removed a lot of code to get into details here.
Have a Space pointer (smart or raw). Instantiate the specific space that you want, point to it with your pointer of type Space. Example std::shared_ptr<Space> pointerToSpace = std::make_shared<Snake> ("I'm a snake"); Now, without loss of generality, you can print the contents (of concrete type) with just the pointer to the space pointerToSpace->class_type(). Yes, you can have a collection of shared_ptrs in a container.

A vector for different classes

I have a class with a vector I'd like to fill with one of two types of class, selected by the user. Let's call my classes option1 and option2
What I'd like to do it something like
class storage_class
{
public:
storage_class(int sel, int n)
{
if(sel == 1)
for(int i = 0; i < n; i++)
my_store.push_back(std::make_unique<option1>());
else if(sel == 2)
for(int i = 0; i < n; i++)
my_store.push_back(std::make_unique<option2>());
}
private:
// Something like this but that actually works
std::vector<T> my_store;
};
Then I'd like to use it like this, or something similar, so there's no need to modify this usage dependent on the option chosen.
int main()
{
storage_class store(1);
int n_iterations = 4;
for(int i = 0; i < n_iterations; i++)
{
store.my_store[i]->create_data();
}
}
The classes option1 and option2 will be mathematical simulations that will be creating data and themselves store this data in a vector that are members of the class.
I want to store multiple instances of either option in a vector and then manipulate them from there. I can use C++17.
As you have c++17 in use, you can simply use a std::variant as type for the container which itself can keep all types you want to have.
Example:
class A { public: void Do() { std::cout << "A::Do" << std::endl; } };
class B { public: void Go() { std::cout << "B::Go" << std::endl; } };
template<class... Ts> struct funcs : Ts... { using Ts::operator()...; };
template<class... Ts> funcs(Ts...) -> funcs<Ts...>;
int main()
{
std::vector<std::variant<A,B>> vec;
vec.push_back(A{});
vec.push_back(B{});
for ( auto& el: vec)
{
std::visit( funcs{ [](A& a){ a.Do(); }, [](B& b) { b.Go(); } }, el);
}
}
Output:
A::Do
B::Go
The classes are fully independent and the methods can be simply called with std::visit and passing a callable object herein. I provide a simple funcs implementation, which simply collect all callable entities to simplify to interface the call to different methods of different unrelated classes here.
As std::variant is some kind of a tagged union, it needs the storage for the biggest type you have in use. If this wastes to much memory, you can store a pointer to the instance instead, maybe with std::unique_ptr or std::shared_ptr if you like some assistance for memory management ;)
Here is an example that tries to stay as close to your example as it can using a template parameter on class storage_class. See working version here. I've added only option1 and made the member my_store public as you access it in your main function.
#include <memory>
#include <vector>
#include <iostream>
struct option1{
void create_data(){ std::cout << "created\n"; }
};
template<typename T>
class storage_class
{
public:
storage_class(int n)
{
for(int i = 0; i < n; i++)
my_store.push_back(std::make_unique<T>());
}
std::vector<std::unique_ptr<T>> my_store;
};
int main()
{
storage_class<option1> store(4);
int n_iterations = 4;
for(int i = 0; i < n_iterations; i++)
{
store.my_store[i]->create_data();
}
}
another option would be to use std::variant. See workign version here.
#include <memory>
#include <vector>
#include <variant>
#include <iostream>
struct option1{
void create_data(){ std::cout << "created 1\n"; }
};
struct option2{
void create_data(){ std::cout << "created 2\n"; }
};
class storage_class
{
public:
using option = std::variant<std::unique_ptr<option1>,std::unique_ptr<option2>>;
storage_class(int sel, int n)
{
if(sel == 0)
for(int i = 0; i < n; i++)
my_store.push_back(option(std::make_unique<option1>()));
else if(sel == 1)
for(int i = 0; i < n; i++)
my_store.push_back(option(std::make_unique<option2>()));
}
std::vector<option> my_store;
};
int main()
{
storage_class store(1, 4);
int n_iterations = 4;
for(int i = 0; i < n_iterations; i++)
{
std::get<1>(store.my_store[i])->create_data();
}
}
Standard way is to make option1 and option2 derived classes from a base_class which seems consistent with your sample main(). Using a generic Factory class template, here is an example:
#include <functional>
#include <iostream>
#include <memory>
#include <unordered_map>
#include <vector>
// Generic Factory class template
template<typename K,typename T,typename... Ts>
class Factory
{
using Map = std::unordered_map<K, std::function<std::unique_ptr<T>(Ts...)>>;
const Map mMap;
public:
Factory(Map&& map):mMap(std::move(map)) { }
std::unique_ptr<T> operator()(const K& key, Ts... args) const
{
const typename Map::const_iterator itr = mMap.find(key);
return itr == mMap.cend() ? nullptr : itr->second(std::forward<Ts>(args)...);
}
};
class base_class
{
public:
virtual void create_data() = 0;
};
class option1 : public base_class
{
public:
void create_data() override
{
std::cout << "I'm option1." << std::endl;
}
};
class option2 : public base_class
{
public:
void create_data() override
{
std::cout << "I'm option2." << std::endl;
}
};
class storage_class
{
using SimulationFactory = Factory<int,base_class>; // Optionally add constructor parameter types
const SimulationFactory simulation_factory; // This can be made static const.
public:
storage_class(int sel, int n)
: simulation_factory(
{ { 1, []() { return std::make_unique<option1>(); } }
, { 2, []() { return std::make_unique<option2>(); } }
})
{
for (int i = 0; i < n; i++)
my_store.push_back(simulation_factory(sel));
}
std::vector<std::unique_ptr<base_class>> my_store;
};
int main()
{
int n_iterations = 4;
storage_class store(1, n_iterations);
for(int i = 0; i < n_iterations; i++)
{
store.my_store[i]->create_data();
}
}
This compiled for me on linux using g++ -std=c++17 main.cc.
There are improvements that can be made to this code, but I copied your main() function in order to illustrate the basic idea(s). Hope that helps.
Edit 21 Sept 2018 - Example of how to pass parameters into constructors.
File: factory.h
#pragma once
#include <functional>
#include <memory>
#include <unordered_map>
// Generic Factory class template
template<typename K,typename T,typename... Ts>
class Factory
{
using Map = std::unordered_map<K, std::function<std::unique_ptr<T>(Ts...)>>;
const Map mMap;
public:
Factory(Map&& map):mMap(std::move(map)) { }
std::unique_ptr<T> operator()(const K& key, Ts... args) const
{
const typename Map::const_iterator itr = mMap.find(key);
return itr == mMap.cend() ? nullptr : itr->second(std::forward<Ts>(args)...);
}
};
File: main.cc
#include "factory.h"
#include <iostream>
#include <string>
#include <vector>
class base_class
{
public:
virtual void create_data() = 0;
};
class option1 : public base_class
{
const double mD;
public:
option1(double d)
: mD(d)
{ }
void create_data() override
{
std::cout << "I'm option1: mD("<<mD<<')' << std::endl;
}
};
class option2 : public base_class
{
const double mD;
public:
option2(double d)
: mD(d)
{ }
void create_data() override
{
std::cout << "I'm option2: mD("<<mD<<')' << std::endl;
}
};
class storage_class
{
using SimulationFactory = Factory<int,base_class,double>; // Optionally add constructor parameter types
const SimulationFactory simulation_factory; // This can be made static const.
public:
storage_class(int sel, int n)
: simulation_factory(
{ { 1, [](double d) { return std::make_unique<option1>(d); } }
, { 2, [](double d) { return std::make_unique<option2>(d); } }
})
{
for (int i = 0; i < n; i++)
my_store.push_back(simulation_factory(sel,static_cast<double>(i)));
}
std::vector<std::unique_ptr<base_class>> my_store;
};
int main()
{
int n_iterations = 4;
storage_class store1(1, n_iterations);
storage_class store2(2, n_iterations);
for(int i = 0; i < n_iterations; i++)
{
store1.my_store[i]->create_data();
store2.my_store[i]->create_data();
}
}
Output:
I'm option1: mD(0)
I'm option2: mD(0)
I'm option1: mD(1)
I'm option2: mD(1)
I'm option1: mD(2)
I'm option2: mD(2)
I'm option1: mD(3)
I'm option2: mD(3)

Inheritance , how can i make two classes share the same base class contents?

I have an Abstract Class operations that inherits from VAR Class , which then all the operations derived class(out,sleep,Add) inherit from the operations class. FSM Class inherits from Var also, so That I want one instance of VAR class inside my program.
I am trying to make vector < pair< string, int>> var as a shared data between the FSM class and the Operations class and its deviates . I initialized the var in the main through the FSM class .
Each time we call the exist function in VAR through Class operation , it returns it doesn't exits cause it is empty ! How can I overcome this?
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class VAR
{
public:
vector<pair<string, int>> var;
VAR()
{
cout << "created VAR" << endl;
}
~VAR(){ cout << "Destrioed VAR" << endl; }
void createVar(string x,int y)
{
pair<string, int>t;
t.first = x;
t.second = y;
var.push_back(t);
}
int getVarValue(string x)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == x)
{
return var[i].second;
}
}
}
void setVarValue(string& x, int y)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == x)
{
var[i].second = y;
i = var.size();
}
}
}
bool exits(string& name)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == name)
return true;
}
return false;
}
};
class operations : virtual public VAR
{
public:
operations()
{
cout << "operations created" << endl;
}
~operations()
{
cout << "operations Destroied" << endl;
}
void virtual excute() = 0;
};
class Out :public virtual operations
{
public:
string s;
Out(string xx = "") :s(xx)
{
cout << "Out created" << endl;
}
~Out()
{
cout << "Out Destroied" << endl;
}
void virtual excute()
{
cout << "out Class" << endl;
if (exits(s))
cout<<"it never reach here, WHY !"<<endl;
}
};
class Add :public virtual operations
{
public:
string s;
Add(string ss = "") :s(ss)
{
cout << "ADD created" << endl;
}
~Add()
{
cout << "Add Destroied" << endl;
}
void virtual excute()
{
string ex1 = s.substr(s.find('=') + 1, s.find('+')), ex2 = s.substr(s.find('+') + 1);
if (exits(ex1))
cout<<"it never reach here, WHY !"<<endl;
else
result = atoi(ex1.c_str());
if (exits(ex2))
cout<<"it never reach here, WHY !"<<endl;
}
};
class state
{
public:
vector<operations*> instructionList;
string name;
void exec_all()
{
for (int x = 0; x < instructionList.size(); x++)
instructionList[x]->excute();
}
};
class transition
{
public:
vector < pair<state, vector<pair<state, int>>>> trans;
static int currentState;
};
class FSM :public virtual VAR, public virtual transition
{
public:
FSM()
{
cout << "FSM" << endl;
}
void intialize()
{
createVar("X", 1);
createVar("Y", 5);
}
};
void main()
{
FSM x;
pair<state, vector<pair<state, int>>> p1;
pair<state, int>p2;
x.intialize();
p2.first.name = "b";
p2.second = 3;
p1.first.name = "a";
p1.second.push_back(p2);
x.trans.push_back(p1);
x.trans[0].first.instructionList.push_back(new Add("X=X+Y"));
x.trans[0].first.instructionList.push_back(new Out("X"));
x.trans[0].first.exec_all();//wrong output cause exist() returns false
}
A minimal complete example looks something like this:
#include <iostream>
using namespace std;
class VAR
{
public:
int var;
virtual ~VAR()
{}
void setVar(int n)
{var=n;}
};
class Out :public VAR
{};
class FSM :public VAR
{};
int main()
{
FSM x;
x.setVar(5);
Out OP;
if (x.var==OP.var)
cout<<"it reaches here now" << endl;
else
cout << "it fails" << endl;
return(0);
}
And one way to fix it is like this:
class VAR
{
public:
static int var;
int var;
virtual ~VAR()
{}
void setVar(int n)
{var=n;}
};
int VAR::var=0;

C++ function pointer (class member) to non-static member function

class Foo {
public:
Foo() { do_something = &Foo::func_x; }
int (Foo::*do_something)(int); // function pointer to class member function
void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; }
private:
int func_x(int m) { return m *= 5; }
int func_y(int n) { return n *= 6; }
};
int
main()
{
Foo f;
f.setFunc(false);
return (f.*do_something)(5); // <- Not ok. Compile error.
}
How can I get this to work?
class A{
public:
typedef int (A::*method)();
method p;
A(){
p = &A::foo;
(this->*p)(); // <- trick 1, inner call
}
int foo(){
printf("foo\n");
return 0;
}
};
void main()
{
A a;
(a.*a.p)(); // <- trick 2, outer call
}
The line you want is
return (f.*f.do_something)(5);
(That compiles -- I've tried it)
"*f.do_something" refers to the pointer itself --- "f" tells us where to get the do_something value from. But we still need to give an object that will be the this pointer when we call the function. That's why we need the "f." prefix.
class A {
int var;
int var2;
public:
void setVar(int v);
int getVar();
void setVar2(int v);
int getVar2();
typedef int (A::*_fVar)();
_fVar fvar;
void setFvar(_fVar afvar) { fvar = afvar; }
void insideCall() { (this->*fvar)(); }
};
void A::setVar(int v)
{
var = v;
}
int A::getVar()
{
std::cout << "A::getVar() is called. var = " << var << std::endl;
return var;
}
void A::setVar2(int v2)
{
var2 = v2;
}
int A::getVar2()
{
std::cout << "A::getVar2() is called. var2 = " << var2 << std::endl;
return var2;
}
int main()
{
A a;
a.setVar(3);
a.setVar2(5);
// a.fvar = &A::getVar;
a.setFvar(&A::getVar);
(a.*a.fvar)();
a.setFvar(&A::getVar2);
(a.*a.fvar)();
a.setFvar(&A::getVar);
a.insideCall();
a.setFvar(&A::getVar2);
a.insideCall();
return 0;
}
I extended Nick Dandoulakis's answer. Thank you.
I added a function which set the member function pointer from outside of the class. I added another function which can be called from outside to show inner call of member function pointer.
Try (f.*do_something)(5);
#include<iostream>
using namespace std;
class A {
public:
void hello()
{
cout << "hello" << endl;
};
int x = 0;
};
void main(void)
{
//pointer
A * a = new A;
void(A::*pfun)() = &A::hello;
int A::*v1 = &A::x;
(a->*pfun)();
a->*v1 = 100;
cout << a->*v1 << endl << endl;
//-----------------------------
A b;
void(A::*fun)() = &A::hello;
int A::*v2 = &A::x;
(b.*fun)();
b.*v2 = 200;
cout << b.*v2 << endl;
}
I think calling a non static member of the class could also be done using a static member function.