Skip to main content
gamedev.net
🔒 Locked

C++ Scope Question [SOLVED]

Started by BrianMJC Jun 15, 2006 at 2:37 PM 3 replies 1.2k views
Original Post
BrianMJC
BrianMJC
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.

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
  }


Thanks for any help! [Edited by - BrianMJC on June 15, 2006 4:05:12 PM]
Enigma
Enigma
Your problem is not scope, but type. Your func_X functions are all member functions. Member functions are different to normal functions - they require an implicit this parameter for one - and a pointer-to-member-function cannot be converted to a pointer-to-normal-function. Try looking into Boost.Function for a more flexible approach involving function objects.

Σnigma
snk_kid
snk_kid
pointer to non-static member function != pointer to function
BrianMJC
BrianMJC
Okay, thanks everyone - I thought there might be a way since I know you can have regular function pointers that point to class member functions (e.g. int (someclass::*fnptr)(); ).

EDIT: Okay, I see now - using function objects is the way. :) Thanks!

[Edited by - BrianMJC on June 15, 2006 4:26:27 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.