final methods/functions

S

Shea Martin

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
 
T

Tobias Blomkvist

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
 
S

Shea Martin

Tobias said:
Shea Martin sade:



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
 
T

Tobias Blomkvist

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
 
D

dragoncoder

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

Aaron W. LaFramboise

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
 
S

Shea Martin

dragoncoder said:
No it can't.



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/d...clang/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.

~S
 
S

Shea Martin

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

Cheers,

~S
 
S

Shea Martin

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


~S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top