my preference is not to use static const members as it always seem to create more coupling than const members; sometimes confusion w.r.t diamond inheritance hierarchy where the final class inherits from 2 super classes (1 class is inherited public; the other is inherited virtual public) then defining a static const member that points to a dynamic memory region via new, malloc, calloc etc would result in a double free error.
e.g. here's the output of a simple diamond inheritance situation
ray:~ ray$ ./multiinheritance
Base() called
Derived1() called
Base() called
Derived2() called
Final() called
~Final() called
~Derived2() called
~Base() called
freeing memory
~Derived1() called
~Base() called
freeing memory
multiinheritance(475) malloc: *** error for object 0x100150: double free
*** set a breakpoint in malloc_error_break to debug
ray:~ ray$
Here's the code:
`
#include <iostream>
#include <string>
class Base {
public:
Base() {
std::cout << "Base() called " << std::endl;
}
virtual ~Base() {
std::cout << "~Base() called" << std::endl;
std::cout << "freeing memory" << std::endl;
delete i;
}
static const int* i;
};
const int* Base::i = new int[5];
class Derived1 : virtual public Base {
public:
Derived1() {
std::cout << "Derived1() called " << std::endl;
}
virtual ~Derived1() {
std::cout << "~Derived1() called" << std::endl;
}
};
class Derived2 : public Base {
public:
Derived2() {
std::cout << "Derived2() called " << std::endl;
}
virtual ~Derived2() {
std::cout << "~Derived2() called" << std::endl;
}
};
class Final: public Derived1, public Derived2 {
public:
Final() {
std::cout << "Final() called" << std::endl;
}
~Final() {
std::cout << "~Final() called" << std::endl;
}
};
int main(int argc, char** argv) {
Final f;
return 0;
}
`