[sldev] Question on Overuse of virtual in C++

Dirk Moerenhout blakar at gmail.com
Sun Jul 1 11:07:12 PDT 2007


For starters: I'm no C++ guru so I might be missing something. Feel
free to enlighten me if so ;-)

My question is the following:
Is there a purpose to declare a function virtual if you've no
intention to overload it? As an example I'd like to take LLTreeNode.

In LLTreeNode we've "virtual LLTreeListener<T>* getListener(U32 index)
const { return mListeners[index]; }"

This translates into a very small piece of code but it'll not inline
due to the virtual. It's declared immediately as part of the class
which is known to help in convincing the compiler to inline but in
VS2005 it won't by default. If I dig thru the code I can not find any
derived class that overloads getListener. Even better in LLOCTreeNode
we get:

virtual oct_listener* getOctListener(U32 index)
{
        return (oct_listener*) BaseType::getListener(index);
}

which makes it look even more strange to me.

I can imagine it has something to do with the fact that we're using
template classes but reading what I can find on template classes I
still don't understand the reasoning.

In the end, removing the virtual does achieve what I want, it inlines
the function and SL seems to run just fine.

Anybody care to explain?

Dirk aka Blakar Ogre


More information about the SLDev mailing list