Is it correct, following object orientation, to use pointers to C++ functions?

Asked

Viewed 304 times

7

I am creating classes that represent items in a menu, each item performs an action and I am assigning the action by a function pointer to each instantiated menu item, is this valid by following POO? or there is an easier and more practical way to do this?

Follows code:

class MenuItem{
public:
     MenuItem(GLsizei x, GLsizei y, string texto, bool enabled = false);
     ~MenuItem();

     void display();

     void setAction(void (*action)());
     void runAction();
private:
     GLsizei x, y;
     string texto;
     bool enabled;
     void (*action)();
};

void MenuItem::setAction(void (*action)())
{
     this->action=action;
}

void MenuItem::runAction()
{
    action();
}

Class that instance Menuitem:

class Menu{
public:
     Menu(vector<string> itens, GLsizei width, GLsizei heigth);
     ~Menu();

     void selectNext();
     void selectPrevious();

     MenuItem* getSelectedItem() const;

     void display();
private:
     MenuItem **itens;
     int selectedItem;
     int contItens;
     GLsizei width, heigth;
};


void function(void){
     cout<<"faz alguma coisa"<<endl;
}

Menu::Menu(vector<string> itens, GLsizei width, GLsizei heigth)
     :width(width), heigth(heigth)
{
     contItens=0;
     this->itens=new MenuItem*[itens.size()];
     for(string s: itens){
         this->itens[contItens++]=new MenuItem(width/2, heigth/2-contItens*30, s);
     }
     this->itens[selectedItem=0]->setEnabled(true);

     this->itens[0]->setAction(function);//aqui eu coloco a ação no primeiro item
}

calling function:

void keyboard(unsigned char key, int x, int y){
    if(key=='s')
        menu->getSelectedItem()->runAction();
    if(key=='d')
        menu->selectNext();
    if(key=='a')
        menu->selectPrevious();
    glutPostRedisplay();
}

I only created function for testing. My idea is to put a method in Menu which places by means of function pointer the action referenced to each item. But the code itself works, I’m just in doubt if it follows POO and if it’s not so complex?

  • 1

    for menus has the iterator pattern.. then it creates a list, or dictionary to organize the items... I don’t know if this answers your question

  • I am creating my own menu as I am doing an openGL project.

  • What kind of application? for example a game. Could you show us some code?

1 answer

5


Correct can only be said by seeing a concrete case. In this respect the question cannot be answered. The actual case has not been put forward, so even if I say you can do it, if you do it wrong it doesn’t matter what I said.

This has nothing to do with object orientation. Even if it had, what matters is to solve the problem well, not meet what the paradigm says.

Pointers for functions are perfectly valid in various situations.

Of course in C++ it is more common to use functors or more (not so) recently Amble. You can also make creative uses of templates or the simple polymorphism. If you think you need this indirect the ideal is to use a more idiomatic mechanism of C++ and more modern (because it brings advantages).

  • No matter the language I always come across this problem adopt the paradigm 100% form. Even in java we end up forcing us to take actions that escape the use of pure OO. The problem I see is the motivation, sometimes we need to give up the elegance in search of a better performance, other times the standardization in the name of readability so that there is not a very complex and deep code, with many classes being called levels forming for a particular task. However technical it may seem I have already opted in a multi-tier system, skip some to reduce the Qtd of interactions

  • 1

    @That’s more or less what Delfino is. It may be useful: http://answall.com/q/141624/101

  • I read the suggested answer and it is quite instructive, and it is a reading that deserves to be redone more calmly. But undoubtedly C++ is the most versatile and difficult language to define only as OO. I would love to meet and test with Smaltalk one day.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.