473,748 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

final methods/functions

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

SaveDataWithWar n 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
8 1569
Shea Martin sade:
I have a pure virtual f'n bool SaveData(). This f'n is called by
SaveDataWithWar n(). The idea being the implementing class must define
how data is saved, but the base class handles GUI operations, and user
interaction.

SaveDataWithWar n 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 SaveDataWithWar n 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
Tobias Blomkvist wrote:
Shea Martin sade:
I have a pure virtual f'n bool SaveData(). This f'n is called by
SaveDataWithWar n(). The idea being the implementing class must define
how data is saved, but the base class handles GUI operations, and user
interaction.

SaveDataWithWar n 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 SaveDataWithWar n 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
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 SaveDataWithWar n
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
> 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
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
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
Tobias Blomkvist wrote:
Since you mentioned a pure virtual function, I assume your question is
based on polymorphism, and if you omit virtual from SaveDataWithWar n
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. SaveDataWithWar n 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5308
by: G. Ralph Kuntz, MD | last post by:
Step on my soapbox... :-) I believe that the "final" keyword is underused in Java. I have gotten into the habit of using it on EVERY method parameter, instance variable, and local variable unless I have reason not to do so. I also use it on class definitions although one could argue that that defeats the idea behind code reuse (I have the source code and can change it to not be final if necessary). I also believe that...
99
5912
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
0
2350
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
14
23140
by: Medi Montaseri | last post by:
Hi, I think my problem is indeed "how to implement something like java's final in C++" The long version.... I have an abstract base class which is inherited by several concrete classes. I have a group of methods that I'd like to implement in the base class
4
2979
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
5
1715
by: Hitesh Patel | last post by:
hi friends, read following code and my problem. class B { private: public: void fun1(void) {
193
9616
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
14
1770
by: My4thPersonality | last post by:
Has the fact that both Java and C# are garbage collected, and C++ in not, anything to do with the fact that there is no language item to prevent a class from being inherired from? I once read that Java and C# implement this feature for preformance, but the C++ creators said it was not worse the effort. So because Java and C# are garbage collected, in their case is it worse the effort? What is the connection?
14
2985
by: Rahul | last post by:
Hi Everyone, I was searching for final class in c++, and i came across few links which suggests to have the constructor of the, to be final class, as private so that any derived class's constructors can't access the same. class C { private:
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.