I just mean that your indentation style in your second code post is different than your first one. You seem to have changed styles for some reason...
Anyway, how my solution gets compiled is rather complicated. It makes use of a
virtual function, so a virtual function table has to be created, so I don't think it can get
inlined.
The problem is that the compiler can't know, at compile time, what function to call. For instance, take this piece of code:
Code:
void do_something_that_includes_printing(dog* dog_obj) {
// do stuff...
// how can the compiler know which dog this is?
dog_obj->print_data();
}
int main() {
dog* dog_obj = new collie();
do_something_that_includes_printing(obj);
delete dog_obj;
dog_obj = new spaniel(); // reusing the pointer
do_something_that_includes_printing(obj);
delete dog_obj;
return 0;
}
The only clue the compiler has as to what version of get_array_data() to use (in print_data(), used in do_something_that_includes_printing()) is the dog_obj
pointer. However, it's only a pointer to the generic dog
class, so it must make use of its virtual function table. This excludes the option of inlining it...
All
member functions,
static or not, are shared across instances of objects. It wouldn't be much of a function if it weren't (the whole point of a function is to
reuse code!).
A
static member function is a
member function that lacks a
this pointer. What that means is that, while it shares the same
access privileges as other member functions, it can't access any member variables. Pretty funny, eh? Well, it makes more sense than it looks, at first...
...edited for clarity...