I was wondering if it is good programming to call a function from a constructor? For example:
class Foo{
Foo(){
function1();
}
void function1(){
}
};
I was wondering if it is good programming to call a function from a constructor? For example:
class Foo{
Foo(){
function1();
}
void function1(){
}
};
If you want pure initialization function, you can handle that (in some cases) in a default constructor
class C
{
C() { /* default init */ }
C(int a) : C() { /* do something extra with a */ }
C(const std::string& s) : C() { /* do something extra with s */ }
};
This is definitely acceptable. Just make sure it is not a virtual function.
EDIT:
"virtual" not "pure virtual"
This is a two part answer. From a technical point of view, sure it's fine, as long as it's not a virtual function.
From a conceptual point of view, it depends what the function does. If all it does is initialisation, then that's what your constructor is suppose to be doing. If it does more, and the function as a purpose outside of that and is being called from elsewhere also, then yeah, it can be a good thing.