Pass Class Name by C ++ parameter

1

I'm having a problem, I need to pass the name of a Class via parameter, so that the function can create a new instance, this Class is the daughter of another Class called Control. The problem is that there is no way to know how many children this Class will have, because if I had I could make an IF for each one of them. But as who will create these Classes will not be I can not control, not to mention that it would be horrible to have to change the function every time a new Class was added or removed. What I need to do in syntax is this:
I will call a Add function of the Validation class, and pass Authentication i> . The Validation class will add this Authentication class in a list, you will just call it at a certain time. At this point, the Validation class will instantiate the Authentication class and call a superscript method of the Control void load () .
How can this be done? This is my idea  Is it correct or does it have a better way of doing this?
Thank you in advance. Home Current class:

#include "Routes.h"

void Routes::path(){

    this->GET("/", IndexController::GetIndex());
    this->POST("/Login", LoginController::GetIndex());
    this->GET("/Entrar", IndexController::GetIndex());


    this->GET("/Dasboard", IndexController::GetIndex());


}

Class How do I need to:

#include "Routes.h"

void Routes::path(){

    this->GET("/", IndexController::GetIndex(), Autenticacao);
    this->POST("/Login", LoginController::GetIndex(), Autenticacao);
    this->GET("/Entrar", IndexController::GetIndex(), Autenticacao);


    this->GET("/Dasboard", IndexController::GetIndex(), Autenticacao);


}

Authentication class:

void Autenticacao::controlar(){
    if(Session::getInstance()->get("usuario") != NULL){
        Header::getInstance()->goTo("/Dashboard");
    }else{
        if(Request::getInstance()->page != "/Entrar"){
            Header::getInstance()->goTo("/Entrar");
        }
    }
}

Route class responsible for POST and GET (Current) method:

void Route::POST(string Path, View* page){
    map<string, map<string, View* > > page_;
    map<string, View*> request_;

    request_["POST"] = page;

    page_[Path] = request_;

    this->routes.push_back(page_);
}

Route class responsible for POST and GET method (How should it be):

void Route::POST(string Path, View* page, Middleware controle){
    map<string, map<string, View* > > page_;
    map<string, View*> request_;

    request_["POST"] = page;

    page_[Path] = request_;

    {Fazer alguma coisa com "controle"}

    this->routes.push_back(page_);
}

Route class when loading some function (Current):

View * Route::load(string nome){

    for (auto& item: routes) {
        for (auto& mapa: item) {
            if(nome == mapa.first){
                for(auto& mapa_ : mapa.second){
                    if(mapa_.first == Server::METHOD()){
                        return mapa_.second;
                    }
                }
            }
        }
    }

    return new class Erro404();

}

Route class when loading some function (How it should stay):

View * Route::load(string nome){

    for (auto& item: routes) {
        for (auto& mapa: item) {
            if(nome == mapa.first){
                for(auto& mapa_ : mapa.second){
                    if(mapa_.first == Server::METHOD()){
                        {pega de algum jeito a classe filho da "Middleware"}->controle();
                        return mapa_.second;
                    }
                }
            }
        }
    }

    return new class Erro404();

}
    
asked by anonymous 17.04.2016 / 22:40

1 answer

0

I solved the problem as follows. Create a Classes responsible for the Controls and I inherited them in the Authentication Class and the other controls. I pass an instance of it via parameter and call it as needed as well as Control because I override some of it's methods.

#include "Routes.h"

void Routes::path(){

    this->GET("/", IndexController::GetIndex(), new Autenticacao);
    this->POST("/Login", LoginController::GetIndex(), new Autenticacao);
    this->GET("/Entrar", IndexController::GetIndex(), new Autenticacao);


    this->GET("/Dasboard", IndexController::GetIndex(), new Autenticacao);
}


#ifndef Autenticacao_CLASS_H
#define Autenticacao_CLASS_H

using namespace std;

class Autenticacao : public Controle {

    public:
        Autenticacao();
        ~Autenticacao();

        virtual void controlar();

    private:
        string login = "/Entrar";
        string dashboard = "/Dashboard";


};

#endif


#ifndef Controle_CLASS_H
#define Controle_CLASS_H

using namespace std;

class Controle {

    public:
        Controle();
        ~Controle();

        virtual void controlar() = 0;
};

#endif
    
19.04.2016 / 13:58