Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > final methods/functions

Reply
Thread Tools

final methods/functions

 
 
Shea Martin
Guest
Posts: n/a
 
      07-25-2005
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.

Thanks,

~S
 
Reply With Quote
 
 
 
 
Tobias Blomkvist
Guest
Posts: n/a
 
      07-25-2005
Shea Martin sade:
> 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.
>
> Thanks,
>
> ~S


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

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
 
Reply With Quote
 
 
 
 
Shea Martin
Guest
Posts: n/a
 
      07-25-2005
Tobias Blomkvist wrote:
> Shea Martin sade:
>
>> 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.
>>
>> Thanks,
>>
>> ~S

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


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.

~S
 
Reply With Quote
 
Tobias Blomkvist
Guest
Posts: n/a
 
      07-25-2005
Shea Martin sade:
>
> 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.
>
> ~S


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.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
 
Reply With Quote
 
dragoncoder
Guest
Posts: n/a
 
      07-25-2005
> Sure it can. Try this:

No it can't.

> 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.

 
Reply With Quote
 
Aaron W. LaFramboise
Guest
Posts: n/a
 
      07-25-2005
On Mon, 25 Jul 2005 14:59:57 GMT, Shea Martin <(E-Mail Removed)>
wrote:

>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.

Aaron W. LaFramboise

 
Reply With Quote
 
Shea Martin
Guest
Posts: n/a
 
      07-26-2005
dragoncoder wrote:
>>Sure it can. Try this:

>
>
> No it can't.
>
>
>>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.

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/de...verloading.asp
or
http://www.maxwell.ph.kcl.ac.uk/~cou...731/C8.html#fo
or
http://valis.cs.uiuc.edu/~sariel/tea.../99_02_24.html

Maybe you should ask them what they are talking about.

~S
 
Reply With Quote
 
Shea Martin
Guest
Posts: n/a
 
      07-26-2005
Tobias Blomkvist wrote:
> 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.

Cheers,

~S
 
Reply With Quote
 
Shea Martin
Guest
Posts: n/a
 
      07-26-2005
Aaron W. LaFramboise wrote:
> 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.


~S
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word open as Final, not Final Showing Markup Karl Engel Computer Support 4 12-19-2006 04:20 AM
Non-final class at compile-time, final at runtime pietdejong@gmail.com Java 5 11-17-2006 12:30 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Extensions and Final Version of Firefox totsob Firefox 1 11-12-2004 01:27 PM
Final release Firefox 1.0 John Firefox 6 11-09-2004 08:46 PM



Advertisments