Jump to content
  • Advertisement
Sign in to follow this  
BrianMJC

C++ Scope Question [SOLVED]

This topic is 4418 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

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]

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!