View Single Post
Old 05-10-2004, 12:01 PM   #10 (permalink)
KnifeMissile
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
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...

Last edited by KnifeMissile; 05-16-2004 at 02:56 PM..
KnifeMissile is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73