mercredi 20 juillet 2016

semi-automatic convert type when calling any non-static functions of a certain class

I am seeking a C++ feature that work as followed:-

Within a certain scope, using a custom converter function, C++ can automatically convert all class instances (int, in this example) to another class (Person), in all places that the name and signature of function is obvious.

Example

I have a function for register a cash account to a person.

void Person::addAccount(Account account){...}
//..... a lot of others function of Person

The person has "id". I can query Person object from "id"

Person* PersonManager::getPerson(int id){ .... }

Therefore, I can add an account to a person id like this :-

personManager()->getPerson(5)->addAccount(Account());

This is what I am trying to achieve. :-

class X{
    Person* super_smart_converter$$$(int id){    
        return  personManager()->getPerson(id);
    }

    void f(){
        int id=5;
        id->addAccount(Account()); //<-- I am captivated by how short it can be
        //^ call super_smart_converter$$$ automatically 
        //   because addAccount is a unique name belongs to Person only
    }
}

Do C++ has such feature, or something similar?

Aucun commentaire:

Enregistrer un commentaire