TITLE: What conditions for virtual dispatch? PROBLEM: mbk@caffeine.engr.utk.edu (Matthew Kennel) The reason I would like to avoid a virtual dispatch is this: The 'member function' in question is the operator () to return an element of a 2-d array in standard column-major layout. I.e. I want to return x->data_storage[i+j*x->nr], given x(i,j). "nr" is an attribute giving the number of rows. It is absolutely necessary that this be *inlined*, because only if that happens is there half a chance that the index multiplication will be strength reduced using such a call (provided it can also prove x and hence x->nr loop-invariant), which is what you need to get good performance. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 16 June 95 You get a virtual function dispatch when all of the following are true: 1. The object is accessed via a pointer or reference. 2. The function is virtual. 3. The function is not qualified with its class name. To avoid a virtual function dispatch (and thus allow inlining) you must make one of those conditions false. Example: [ I corrected some problems in the code below. -adc ] class T : public base { public: virtual int f(); // virtual in base as well int g() { return T::f(); } // not virtual }; T t, *p; t.f() // not accessed via pointer or reference p->g() // g is not virtual p->T::f() // f is explicitly qualified If your operator()() does not need to be virtual, you are done. Otherwise, you can define a non-virtual "index" function which explicitly calls the local version of operator()(), just as g calls f in my example. If index and operator()() are declared inline, the compiler may generate` the whole thing inline. Alternatively, if an array object is allocated statically or is auto and you refer to it directly, operator()() need not involve a virtual call, and can be inlined.