Jump to content
  • Advertisement
Sign in to follow this  
shoez

how to call a member function pointer?

This topic is 2782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

typedef struct Car {
void (*run)(Car *lok);
};

void run(car *lok)
{
....
}

int main()
{
Car lol = { run }; // did i assign this address right?
// And how do i call this function using the pointer to it?
Car.run(&car); // is this right?
}

Share this post


Link to post
Share on other sites
Advertisement

typedef struct Car {
void (*run)(Car *lok);
};

void run(car *lok)
{
....
}

int main()
{
Car lol = { run }; // did i assign this address right?
// And how do i call this function using the pointer to it?
Car.run(&car); // is this right?
}


try this(C code):

[source lang="C"]
#include <stdio.h>

typedef struct
{
void (*run)(const char *message);
}Car;

void Run(const char *message)
{
printf(message);
}

int main()
{
Car car;
car.run = Run;

car.run("I am now running\n");

return 0;
}
[/source]

Share this post


Link to post
Share on other sites
or, if you are up to it, try this C++ code =)

[source lang="cpp"]
#include <iostream>
using namespace std;

class CarFunction
{
public:
virtual ~CarFunction(){}
virtual void Execute() = 0;
};

class DriveFunction : public CarFunction
{
virtual void Execute()
{
cout << "I am driving" << endl;
}
};

class ParkFunction : public CarFunction
{
virtual void Execute()
{
cout << "I am parked" << endl;
}
};

// abstract class for car
class Car
{
public:
void SetCarFunction(CarFunction *function)
{
this->func = function;
}

void RunFunction()
{
func->Execute();
}

private:
CarFunction *func;

};


int main()
{
DriveFunction driveFunction;
ParkFunction parkFunction;
Car myCar;

myCar.SetCarFunction(&driveFunction);
myCar.RunFunction();

myCar.SetCarFunction(&parkFunction);
myCar.RunFunction();


return 0;
}
[/source]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!