In a C++ class is there any overhead in having static function instead of a member functions.
class CExample
{
public:
int foo( int a, int b) {
return a + b ;
}
static int bar( int a, int b) {
return a + b ;
}
};
My questions are;
- In this example is foo() or bar() more effecent?.
- Why would I not want to make foo() in to a static function as it does not alter any member variables?
No. Both calls are resolved statically. There may be some overhead in passing the
thispointer to a non-staticfunction, but in this case both will likely be inlined.You wouldn’t, it’s actually good practice to make all methods not bound to an instance
static.