vendredi 13 septembre 2019

Is using non-member function a good practice?

Let's say I have a class where I have multiple functions with similar logic. Since I don't want to repeat myself, I extract the similar logic in a function. Is it good practice to have similarLogic as a non-member function if it does not use any class members? Or is there a better way to do this?

Example with a non-member function:

MyClass.h

class MyClass {
public:
    int func1();
    int func2();
};

MyClass.cpp

int similarLogic(int p_num){
    return 5 + p_num;
}

int MyClass::func1() {
    return similarLogic(1);
}

int MyClass::func2() {
    return similarLogic(2);
}

Aucun commentaire:

Enregistrer un commentaire