0

First off, I would like to say that this is not a duplicate (or at least I don't believe it is) of Is it safe to change a function pointer (std::function) inside a called function? as I am not using std:: functions. It's also not just a function pointer that's being changed; I am using a "normal" pointer to refer to an object which contains several methods, one of which has a call to the object holding the pointer to change it.

This has worked in the past, but I'm not sure if it's supposed to.

The code is as follows:

class entry {
public:
  void handleEvent(byte button_id, byte flags);
  void virtual left(byte type) {}
  void virtual right(byte type) {}
  void virtual center(byte type) {}
  void virtual draw() {}
  void virtual idle() {}
  void virtual load() {}
};
class menu_class {
  entry *object;
public:
  void registerEntry(entry *obj);//sets 'object' from passed argument
  void update();//draws menu and reads buttons to call associated functions in the 'entry' object
};

To use this, I define a child class to inherit from entry and then define the functions there (since each menu has different behavior). I then pass its pointer to a pointer of the parent object's (the one shown here) type in the menu_class object, and that calls the appropriate function when an event occurs (which is done via polling, and repeated calls to update).

To open a different menu, one of the methods in entry will call the registerEntry method of the menu controller. That replaces the pointer to the calling entry object with a pointer to the new menu item object.

Can this cause the program to start executing arbitrary code or miss a return parameter (although I have none here, currently)?

I also have some related questions that I'm not sure count as part of the same question. Should I move the following to a new question:

If I were to create an array of pointers (or an array of small objects containing the pointers), changing a variable used as a index to that array (when calling the object's pointer's method) is fine?

If I were to dynamically allocate the object whose method is being called, what happens if I deallocate it as part of registerEntry when I change to a new menu? (Let's assume that I make no further references to internal variables of that object, but even so I imagine it's probably now working with memory that no longer exists.)

RDragonrydr
  • 181
  • 9
  • It's a little hard to tell from your description exactly what you mean. Can you give a complete example of the code you're asking about? – Nate Eldredge Aug 30 '20 at 17:14
  • What Nate said, but in general what matters are the objects (their lifetime). So reassigning pointers is not a problem, but deallocating objects could very well cause problems. – alain Aug 30 '20 at 17:19
  • I have added more information about the code and have attempted to clarify my explanation. I have removed extra things that both sections do, but have left intact things related to my question. – RDragonrydr Aug 30 '20 at 18:13

0 Answers0