Original Post
Hi all, Is there a way to create a class in C++ that has a function pointer member such that the class can be instantiated with different scope for its function pointer member? I know that the template keyword is used to instantiate a class with different types (e.g. a queue of ints, a queue of strings, etc.). Can this be done with scope as well as type (e.g. one instance has a function pointer to member functions of class A, a second has a function pointer to member functions of class B, etc.)? Let me demonstrate a simplified version of my code to show what exactly I'm doing. The problem is that the scope of the function pointer changes for each derived class. Thanks for any help! [Edited by - BrianMJC on June 15, 2006 4:05:12 PM]
struct func_ptr_struct
{
void (*_fnptr)(); // scope is func_ptr_struct::
// ... other data
}
class base_class
{
// ... other stuff
protected:
vector<func_ptr_struct> _fps;
// ... other stuff
}
class derived_class_1 : public base_class
{
// ... other stuff
private:
void func_a(); // scope is derived_class_1::
void func_b(); // scope is derived_class_1::
// ... other stuff
}
class derived_class_2 : public base_class
{
// ... other stuff
private:
void func_c(); // scope is derived_class_2::
void func_d(); // scope is derived_class_2::
void func_e(); // scope is derived_class_2::
void func_f(); // scope is derived_class_2::
// ... other stuff
}
class derived_class_3 : public base_class
{
// ... other stuff
private:
void func_g(); // scope is derived_class_3::
void func_h(); // scope is derived_class_3::
void func_i(); // scope is derived_class_3::
// ... other stuff
}
derived_class_1::derived_class_1()
{
_fps[0]._fnptr = func_a; // scope resolution error
_fps[1]._fnptr = func_b; // scope resolution error
// ... other stuff
}
derived_class_2::derived_class_2()
{
_fps[0]._fnptr = func_c; // scope resolution error
_fps[1]._fnptr = func_d; // scope resolution error
_fps[2]._fnptr = func_e; // scope resolution error
_fps[3]._fnptr = func_f; // scope resolution error
// ... other stuff
}
derived_class_3::derived_class_3()
{
_fps[0]._fnptr = func_g; // scope resolution error
_fps[1]._fnptr = func_h; // scope resolution error
_fps[2]._fnptr = func_i; // scope resolution error
// ... other stuff
}