Saturday, August 20, 2005

final methods/functions

I have a pure virtual f'n bool SaveData(). This f'n is called by
SaveDataWithWarn(). The idea being the implementing class must define
how data is saved, but the base class handles GUI operations, and user
interaction.

SaveDataWithWarn is called by a GUI event trigger. But it is possible
that an implementing class would like to manually call this function.
Thus it must be atleast public. But I don't want the base class to over
ride it. I believ in Java I can put 'final' as a modifier. Is there an
equiv in C++, besides a comment like this:
//over-riding this function will result in a pink-slip.
- Shea Martin

9 Comments:

At 10:40 AM, August 20, 2005, Anonymous Anonymous said...

Unless SaveDataWithWarn is virtual, it can't be overridden.
Or are you asking something else? I lack a clear picture.

 
At 10:45 AM, August 20, 2005, Anonymous Anonymous said...

Sure it can. Try this:

class A
{
void fn()
{
std::cout << "A::fn()\n";
}

}

class B : A
{
void fn()
{
//something different.
std::cout << "B::fn()\n";
}

}

Virtual just specifies which function gets called when polymorphism is
in action. For example:

B *b = new B();
A *a = 0;

b->fn(); //prints B::fn()
a = b;
a->fn(); //prints A::fn(), because fn() is not virtual
//if fn() was declared virtual in class A,
//then B::fn() would be printed.

 
At 10:47 AM, August 20, 2005, Anonymous Anonymous said...

> Virtual just specifies which function gets called when polymorphism is
> in action. For example:

Exactly.

> B *b = new B();
> A *a = 0;

> b->fn(); //prints B::fn()
> a = b;
> a->fn(); //prints A::fn(), because fn() is not virtual
> //if fn() was declared virtual in class A,
> //then B::fn() would be printed.


Since you mentioned a pure virtual function, I assume your question is
based on polymorphism, and if you omit virtual from SaveDataWithWarn
the derived class can't override it from the polymorphic perspective,
since the base class pointer will always call the base class function.

But as I said, I lack a clear picture; since I can't really follow
the connections within your question.

 
At 10:53 AM, August 20, 2005, Anonymous Anonymous said...

> Sure it can. Try this:

No it can't.

- Hide quoted text -
- Show quoted text -
> class A
> {
> void fn()
> {
> std::cout << "A::fn()\n";
> }

> }
> class B : A
> {
> void fn()
> {
> //something different.
> std::cout << "B::fn()\n";
> }

> }

What you are talking about, is called overloading and not overriding.

> Virtual just specifies which function > gets called when polymorphism is
> in action. For example:

> B *b = new B();
> A *a = 0;

> b->fn(); //prints B::fn()
> a = b;
> a->fn(); //prints A::fn(), because fn() is > not virtual
> //if fn() was declared virtual in > class A,
> //then B::fn() would > be printed.

Thats because fn() would be overriden.

The term "overriding" comes under the context of polymorphysm, which is implemented using virtual keyword.

 
At 10:56 AM, August 20, 2005, Anonymous Anonymous said...

>Virtual just specifies which function gets called when polymorphism is
>in action. For example:

Generally speaking, it is stylistically inappropriate to do this sort of overriding when dealing with polymorphism. In C++, the 'class' mechanism is reused for several different purposes (concrete, abstract bases, mixin implementation, vanilla polymorphic, trait, etc) for each of which certain features are inappropriate to use. You can contrast
this to Java, where classes are user-defined polymorphic types, and
pretty much nothing else.

It's worth pointing out that this non-virtual override is not the same
function as it's parent's function; it just happens to share the same
signature.

Used from the base class type, the object will still operate correctly
and as expected. The only trouble here is if a user is using the
object as the derived type, without consulting the class documentation, and expecting it to work properly. But this is the same with non-virtual overriding or no: blindly using an interface
without consulting the documentation may result in strange things.
Besides, why are they using the derived type if they want guarantees
provided only by the parents?

Since this sort of thing is a misuse anyway, why do you care?

Many compilers can be configured to give a warning for this situation.
It's too bad that many low quality textbooks (and low quality
universities) continue to teach non-virtual overriding, often eliding discussion of 'virtual' itself entirely.

But no, to answer your question, this is not possible without doing
something very invasive, such as parameterizing the base class with
the type of the derived.

 
At 10:59 AM, August 20, 2005, Anonymous Anonymous said...

> Since this sort of thing is a misuse anyway, why do you care?

Good point. Sometimes I get sidetracked...

> Many compilers can be configured to give a warning for this situation.

A 'W' directive... Wall should work.

> It's too bad that many low quality textbooks (and low quality
> universities) continue to teach non-virtual overriding

Very true.

> But no, to answer your question, this is not possible without doing
> something very invasive, such as parameterizing the base class with
> the type of the derived.

Thanks for a great answer.

 
At 11:00 AM, August 20, 2005, Anonymous Anonymous said...

> Since this sort of thing is a misuse anyway, why do you care?

Good point. Sometimes I get sidetracked...

> Many compilers can be configured to give a warning for this situation.

A 'W' directive... Wall should work.

> It's too bad that many low quality textbooks (and low quality
> universities) continue to teach non-virtual overriding

Very true.

> But no, to answer your question, this is not possible without doing
> something very invasive, such as parameterizing the base class with
> the type of the derived.

Thanks for a great answer.

 
At 11:03 AM, August 20, 2005, Anonymous Anonymous said...

> Since you mentioned a pure virtual function, I assume your question is
> based on polymorphism, and if you omit virtual from SaveDataWithWarn
> the derived class can't override it from the polymorphic perspective,
> since the base class pointer will always call the base class function.

Sorry, I guess I should have been more clear. SaveDataWithWarn was the call I was wanting to prevent an implementing class from overriding.

I guess in a sense you are right, if it is not virtual, then my version
(version defined in base class) will always be called.... but if a
downcast is performed, then it would be possible to get the implementing
classes version. Though your solution is still fairly effective.

The solution is still a runtime solution, unlike java's "final", which is a compile time enforcement. Anyway I have my anser now.

 
At 11:06 AM, August 20, 2005, Anonymous Anonymous said...

Oh, I though overloading was having multiple versions of a function,
each version taking different arguments.
see the following examples of what some people call overloading:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_function_overloading.asp
or
http://www.maxwell.ph.kcl.ac.uk/~courses/cp4731/C8.html#fo
or
http://valis.cs.uiuc.edu/~sariel/teach/1998/soft99a/tirgul/99_02_24/99_02_24.html
Maybe you should ask them what they are talking about.

 

Post a Comment

<< Home