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

Chance Unknown chance at kalacia.com
Sun Jul 1 12:41:49 PDT 2007


one use of virtual functions is when you are only provided a library to link
to and you need to override some function that is provided in the library.
the library designer can declare the function as virtual to allow this to
happen. its a design call on the part of the library designer really. the
complaint against virtual functions is that they carry the requirement of
runtime type information that the compiler has to insert stubs for the C++
runtime to account for, for some projects, that overhead is a tad more than
you want to carry.

when you have the sources to modify, virtual functions become more of a
design decision than something that has extremely profound usefulness.
arguments can be made on both sides of the camp on why they are useful, and
why they are evil and must be destroyed.



On 7/1/07, Dirk Moerenhout <blakar at gmail.com> wrote:
>
> 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
> _______________________________________________
> Click here to unsubscribe or manage your list subscription:
> /index.html
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.secondlife.com/pipermail/sldev/attachments/20070701/102dd608/attachment.htm


More information about the SLDev mailing list