Function pointers in classes...
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
February 26, 2000 12:02 AM
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.
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.
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
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:
This code works under: gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
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.
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.
--- Official D Blog | Learning D | The One With D | D Bits
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement