Hello Crow!
For each object of your type there is only one instance of the methods.
The main problem with virtual functions is performance overhead. As far
as the performance is considered, guys @ MS has given it a lot of
thought that's why you can see that methods in .Net are by default
"non-virtual" as opposed to java where every method is
"virtual" by default.
There is one more thing that is catered using the "non-virtual"
approach and that is versioning but this is alone a separate issue. I
think this
(
http://www.parashift.com/c++-faq-lit....html#faq-20.4)
should answer you question.
But you need to keep on eye on one thing that by using only the
"non-virtual" approach you might be sacrificing your design
somewhere at sometime. Let say you have a Style Factory as listed
below:
<code>
public IStyle Create(String type)
{
switch(style)
{
case EMBEDDED_NONE: return new EmbeddedNone();
case EMBEDDED_UNDERSCORE: return new EmbeddedUnderscore();
case EMBEDDED_HYPHENS: return new EmbeddedHyphens();
default: throw new Exception("Unknown Style");
}
}
</code>
If you are using aforementioned approach (i.e. using a non-virtual
factory implementation) than you are downsizing a few design issues
over here. Let say at a latter point you need to add a few more styles
to your repository so then for each new style entry you need to
explicitly change your factory code and mind it with enterprise level
project its not an easy and amusing task at all.
If on the other case you might have use the implementation like this
<code>
protected virtual IStyle Create(String type)
{
switch(style)
{
case EMBEDDED_NONE: return new EmbeddedNone();
case EMBEDDED_UNDERSCORE: return new EmbeddedUnderscore();
case EMBEDDED_HYPHENS: return new EmbeddedHyphens();
default: throw new Exception("Unknown Style");
}
}
</code>
Than at any point in time you can do something like this and this will
give your design a whole new dimension of flexibility and dynamics.
<code>
Protected override IStyle Create(String type)
{
return (IStyle) Activator.CreateInstance(Type.GetType(type));
}
</code>
So remember, "non-virtual" is a GOOD thing to do but it's NOT
always the BEST thing to do.
Hope this might be of some help.
Let me know in case of any inconsistency.
Regards,
Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd