Advertisement

Function pointers in classes...

Started by February 25, 2000 11:56 PM
4 comments, last by Nepheus 24 years, 6 months ago
Hy everybody, my problem is: I have a class that have two function : Plot8 - 256 cores Plot16 - 16 bits well, i need todo: on the initialization of the class i can determine what i should use and i''d like to make a function poiter that call the one choiced. like this: class BlaBla { (* Plot)(x,y,c) - Pointer to the right function ... void Plot8(x, y, c); void Plot16(x, y, z); } I tried but it doesn''t work, i can''t point to the class type... or something... if someone can help, I thanx... Sorry about th English, I''m brazilian... Nepheus
"Eu sou, na verdade, a sombra do que quero ser" - Nepheus
Heya,
From what I know. You''ve got two choices.
Either you can make the member function static or use a friend function before you can bind it to your functions pointer. Otherwise you can just use a plain static C function so its defined in the scope of the class.

class Bla
{
public:
Bla();
void (*Update)();
private:
static void Update1();
friend void Update2();
}

void Bla::Update1(){...}
void Update2() {...}

Bla::Bla()
{
if(something)
Update = &Update1
else
Update = &Update2
}

both of these should work.
Advertisement
The problem with that solution is that none of the members are available to the function.
I don't know if you're not going to have to end up with something like:
class BlaBla
{

void Plot(x,y,c) - uses the right function
...
void Plot8(x, y, c);
void Plot16(x, y, z);

int WichPlot;
}

BlaBla::BlaBla()
{
if(...)
WichPlot=16;
else
WichPlot=8;
}

BlaBla:: Plot(x,y,c)
{
switch(WichPlot)
{
case 8:
Plot8(x,y,c);
break;
case 16:
Plot16(x,y,c);
break;
default:
//error handling? break;
}
}

I can't think of a way to solve it other that this, but there may be way to mess around with the Vtable. Any C++ expert wants to comment?

Boa Sorte...

Edited by - alexmoura on 2/26/00 5:47:31 AM
You can create and assign pointers to member functions, the syntax isn''t entirerly logically consistent, though.

ex:
#include class Switchable {  public:    int Ver1(int arg1);    int Ver2(int arg1);      int (Switchable::*member_func)(int);    int UseMemberFunction(int arg1);};  int Switchable::Ver1(int arg1) {  printf("Ver1 %d\n", arg1);}  int Switchable::Ver2(int arg1) {  printf("Ver2 %d\n", arg1);}  int Switchable::UseMemberFunction(int arg1) {  return (this->*member_func)(arg1);}  int main(int argc, char ** argv) {  Switchable s;  s.member_func = &Switchable::Ver1;  s.UseMemberFunction(3);  s.member_func = &Switchable::Ver2;  s.UseMemberFunction(4);  return 0;} 


This code works under: gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
Instead of using function pointers. what I do is declare a pure virtual class, which declares all the graphics routines I use. Then I inherit from this class with a class for each pixel depth, thus I have: Graphics, Graphics8,Graphics15, etc...

In my main graphics file I declare a global pointer: Graphics *gfx = NULL;

Then, on depending on the bit-depth I instatinate gfx with a new instance of the appropriate sub-class.
Thankyou so much !!! Thanx for all... it helped me a lot !
Valeu Alex...
"Eu sou, na verdade, a sombra do que quero ser" - Nepheus

This topic is closed to new replies.

Advertisement