Jump to content

Function Pointers and Inheritance

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
I am trying to build a "maze generator," and I have run into an interesting problem. I want to give the user the choice of printing the maze to the console, to an output file, or to a GUI screen with OpenGL. I think I have come up with a nifty way of doing that, but I don't know if it is possible. Any help is greatly appreciated!

I have several objects that represent different parts of the maze that inherit from one abstract class, and they all have a choice of draw method: console(), file(), or gui(). Each concrete class has these methods, but each draws a slightly different piece of the maze. I have an array of pointers to the abstract class that are objects of the various concrete subclasses.

I want to pass a function pointer as a parameter to a draw function that will draw each individual part of the maze from the array of pointers to the abstract class. I want to do something like this:

void draw(void (*drawFunc)(ostream &), ostream & out)

{

     for (int i=0; i<abstractArray.length; i++)

          abstractArray[i]->(*drawFunc(out));

}


void pickDraw()

{

     // Let user choose option and store in string option

     if (option == "console")

          draw(console, cout);

     else if (option == "file")

          draw(file, fout);

     else

          draw(gui, cout);

}

I do not know, however, how to pass a pointer to a function inside an unknown subclass of an abstract class. Is it possible? Should I give up on this approach? If anymore clarification is needed, just let me know.

Thank you for the help!

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
You appear to be programming this in C++ since you're treating option as a std::string and are using == to check for equality with a char array. Well, if that's the case I'd instead have a class that represents any drawing object and place there pure virtual functions called "DrawBlahAt(objtype, at)", then subclasses of that class called "Console", "File", and "GUI", and have a main drawing function that can treat each of those objects generically. I'm not exactly sure how your maze generator has been designed up until now, or even where you'd send the stuff you need to draw, but there should be some centralized object/function that gets all the drawing information and draws it to whatever surface it's given.
Wow I changed my sig!

#3
bsteeg

bsteeg

    Newbie

  • Members
  • Pip
  • 3 posts

Quote

I want to give the user the choice of printing the maze to the console, to an output file, or to a GUI screen with OpenGL. I think I have come up with a nifty way of doing that, but I don't know if it is possible.

You can ask the user to choose a option, here a example of it:
void pickDraw()
{
   int input;
   
   cout << "Please enter your number";
   cin >> input;

   getch(input)
   {
      case 1:
        Maze->DrawConsole();
      case 2:
        Maze->WriteText();
      case 3:
        Maze->DrawGL();
   }
}


#4
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Thank you for the help! I messed around with it for a while longer, and I think I have it set up the way I want it. I used both of your suggestions. I allow the user to select which method they want to use to print the maze. Then, to print the maze, I created a draw class that handles each method of drawing.

Thanks again! That helped a lot!