vendredi 3 février 2017

How to ensure that every method of a class calls some other method first?

I have :

class Foo {

public:

void log(){

}

void a(){
    log();
}

void b(){
   log();
}

};

Is there a way that I can have each method of Foo, call log(), but without me having to explicitly type log() as the first line of each function ? I want to do this, so that I can add behaviour to each function without having to go through each function and make sure the call is made, and also so that when I add new functions, the code is automatically added...

Is this even possible ? I can't imagine how to do this with macro's, so not sure where to begin... The only way I have thought of so far, is to add a "pre-build step", so that before compiling I scan the file and edit the source code, but that doesn't seem very intelligent....

EDIT: Just to clarify - I don't want log() to call itself obviously. It doesn't need to be part of the class.

Aucun commentaire:

Enregistrer un commentaire