By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,110 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,110 IT Pros & Developers. It's quick & easy.

final methods/functions

P: n/a
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
Jul 25 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
Jul 25 '05 #2

P: n/a
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
Jul 25 '05 #3

P: n/a
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.
Jul 25 '05 #4

P: n/a
> 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.

Jul 25 '05 #5

P: n/a
On Mon, 25 Jul 2005 14:59:57 GMT, Shea Martin <sh***@hb-studios.com>
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

Jul 25 '05 #6

P: n/a
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
Jul 26 '05 #7

P: n/a
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
Jul 26 '05 #8

P: n/a
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
Jul 26 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.