473,472 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how write a final method in c++ like java ..

class B
{
virtual void m( )
{

}
};

class D : public B
{

}

what should be done so that void m() can not be overriden in D

May 30 '06 #1
14 2215
* mangesh:
class B
{
virtual void m( )
{
}
};

class D : public B
{
}

what should be done so that void m() can not be overriden in D


Remove the word 'virtual'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 30 '06 #2
C++ doesn't support final methods (like in Java).

A virtual function can be redefined by a derived class whenever
required. There is no way to restrict this functionality.

May 30 '06 #3
mangesh wrote:
class B
{
virtual void m( )
{

}
};

class D : public B
{

}

what should be done so that void m() can not be overriden in D

http://www.parashift.com/c++-faq-lit...html#faq-23.12
May 30 '06 #4
Geo

mangesh wrote:
class B
{
virtual void m( )
{

}
};

class D : public B
{

}

what should be done so that void m() can not be overriden in D


To be honest, I've never known why you would want to do this, what
problem does it solve ?

May 30 '06 #5
* shailesh:
C++ doesn't support final methods (like in Java).

A virtual function can be redefined by a derived class whenever
required. There is no way to restrict this functionality.


Simply don't declare it virtual in the first place.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 30 '06 #6
Geo wrote:
mangesh wrote:
class B
{
virtual void m( )
{

}
};

class D : public B
{

}

what should be done so that void m() can not be overriden in D


To be honest, I've never known why you would want to do this, what
problem does it solve ?


Since in Java every (non-static) function is virtual you probably want
to give some indication which functions are supposed to be overriden
and which are not. Similar as with non-virtual functions in C++ (yes I
know there is a difference).

May 30 '06 #7
Geo

Markus Schoder wrote:
Geo wrote:
mangesh wrote:
class B
{
virtual void m( )
{

}
};

class D : public B
{

}

what should be done so that void m() can not be overriden in D


To be honest, I've never known why you would want to do this, what
problem does it solve ?


Since in Java every (non-static) function is virtual you probably want
to give some indication which functions are supposed to be overriden
and which are not. Similar as with non-virtual functions in C++ (yes I
know there is a difference).

But this is a question about C++, what Java does is irrelevant.

May 30 '06 #8

Markus Schoder wrote:
Similar as with non-virtual functions in C++ (yes I
know there is a difference).


A pretty big difference, no? non-virtuals in C++ can still be
overridden but that doesn't function polymorphically.

In my opinion there shouldn't be any such thing as final or non-virtual
functions. I see the reasoning behind it in C++ where you can do
whatever you want and language features are 'removable' (like not using
polymorphism) but from a design standpoint I think it smells bad.

May 30 '06 #9
Noah Roberts wrote:
In my opinion there shouldn't be any such thing as final or non-virtual
functions. I see the reasoning behind it in C++ where you can do
whatever you want and language features are 'removable' (like not using
polymorphism) but from a design standpoint I think it smells bad.


Tell that to the C# guys, who chose to do it like C++, not like Java.

Of course you have to know what you're doing, but maybe people who don't
shouldn't go into programming...

I quite like the separation between polymorphism (where and when you
need it, plus it's explicit and visible) and simple, static, efficient,
encapsulated functions (like with C structs). Puts you in control, and
most of the time you shouldn't inherit all over the place anyway.
May 30 '06 #10

Ulrich Hobelmann wrote:
Noah Roberts wrote:
In my opinion there shouldn't be any such thing as final or non-virtual
functions. I see the reasoning behind it in C++ where you can do
whatever you want and language features are 'removable' (like not using
polymorphism) but from a design standpoint I think it smells bad.
Tell that to the C# guys, who chose to do it like C++, not like Java.


Why should I tell it to the C# guys? I don't care what they do or
don't do.

Of course you have to know what you're doing, but maybe people who don't
shouldn't go into programming...
Heh, ok...right to the personal attacks...don't pass go, don't collect
$200. You're way, way too invested in this language feature.

I quite like the separation between polymorphism (where and when you
need it, plus it's explicit and visible) and simple, static, efficient,
encapsulated functions (like with C structs). Puts you in control, and
most of the time you shouldn't inherit all over the place anyway.


Puts you in control? Debatable. You don't have a lot of control if
you need to inherit from a 3rd party library that can't be altered to
have a virtual function that you need to override. The only person
that has control is the person building the class. Clients have none.
Any function that accepts that class as input is stuck with that
class's implementation and depends on it. Any function you wish to
build that accepts that class as input also has a dependency on that
class's implementation...not just its interface. Breaking that
dependency requires an extra wrapper class and I don't find that simple
or efficient.

Simple and efficient? Well, simple is debatable since lacking of this
feature would remove the necessity to declare functions to be virtual.
It would also remove the necessity of workarounds when you can't
override a function you need to override (see above). On the
efficiency angle I would make mention of premature optimization and
question exactly how expensive you think virtual functions are.

The reasons behind not inheriting all over the place are numerous but
one of the most convincing arguments to me is the fact that it creates
strong dependencies. Non-virtual functions also create or indicate
strong dependencies on a specific functionality rather than an
interface. That has an odor.

May 30 '06 #11
Noah Roberts wrote:
Of course you have to know what you're doing, but maybe people who don't
shouldn't go into programming...
Heh, ok...right to the personal attacks...don't pass go, don't collect
$200. You're way, way too invested in this language feature.


It wasn't meant as a personal attack. I merely acknowledge that C++
(and having virtual and non-virtual functions) is complex, and that a
programmer has to live with that complexity, if they choose the job.
Simple and efficient? Well, simple is debatable since lacking of this
feature would remove the necessity to declare functions to be virtual.
It would also remove the necessity of workarounds when you can't
override a function you need to override (see above). On the
efficiency angle I would make mention of premature optimization and
question exactly how expensive you think virtual functions are.
You have a point, but C++ is designed to allow all optimizations that
the programmer might want. If you want libs with virtual functions,
talk to whoever provides the library.
The reasons behind not inheriting all over the place are numerous but
one of the most convincing arguments to me is the fact that it creates
strong dependencies. Non-virtual functions also create or indicate
strong dependencies on a specific functionality rather than an
interface. That has an odor.


Yes. It always depends how much reusability an interface needs or even has.
May 30 '06 #12

Ulrich Hobelmann wrote:
Noah Roberts wrote:

Simple and efficient? Well, simple is debatable since lacking of this
feature would remove the necessity to declare functions to be virtual.
It would also remove the necessity of workarounds when you can't
override a function you need to override (see above). On the
efficiency angle I would make mention of premature optimization and
question exactly how expensive you think virtual functions are.


You have a point, but C++ is designed to allow all optimizations that
the programmer might want.


Other languages also offer this feature but don't make you declare when
you want polymorphism. For instance the OP compares to Java where the
default behavior is to have a virtual function call and the developer
explicitly states when this isn't wanted. Objective-C, a more dynamic
OO language that requires runtime lookups for all name resolutions,
even provides support for static calls to a specific function in the
form of "IMP" pointers. The default behavior is to use dynamic
function calls (in whatever form the language supports) but to allow
for the optimization when truely needed. IMHO this results in better
code as developers are encouraged to use the less dependant mechanism
as a norm.

So I don't really question the necessity of having the feature
available, but I do question the default behavior of class functions
and I also think that using that feature can indicate questionable
coding practices...which is what having a smell means. Obviously there
are situations in which you actually need that kind of speed and the
dependency it creates is a reasonable sacrifice. As a norm however, I
debate that.

May 30 '06 #13

Noah Roberts wrote:
Markus Schoder wrote:
Similar as with non-virtual functions in C++ (yes I
know there is a difference).
A pretty big difference, no? non-virtuals in C++ can still be
overridden but that doesn't function polymorphically.

I know that it's something weird with C++. It is here mainly for
historical reasons, and should not be used.
So, IMHO, it is not a big difference... It's just a language trap.
Advanced programmers don't fall in, period.
You may say that it is a "huge flaw which totally spoils the language".
If you think so, then turn towards a more clean language. C++ is not a
"clean language designed from scratch". We have to live with it, and in
practice it is not a real problem when you know deeply the language.
It is a problem for teaching it to students, though.
In my opinion there shouldn't be any such thing as final or non-virtual
functions. I see the reasoning behind it in C++ where you can do
whatever you want and language features are 'removable' (like not using
polymorphism) but from a design standpoint I think it smells bad.

OOP is not the only programming paradigm... A huge number of problems
are better solved with another programming paradigm.
In particular, C++ accepts the object-based paradigm, which can be
easily and efficiently used, and doesn't require virtual functions.
And, in C++ you don't pay for what you don't use.
C++ gives the freedom of using any available programming paradigm you
want.
The overhead of a vtable pointer can itself be unacceptable when there
are a lot of small objects allocated.
For instance, a 1 or 2 bytes object may use 8 bytes (because of
vptr+alignment)!
Furthermore, objects such as std::vector would suffer from a huge
overhead (500% sometimes) for very simple inline operations such as
operator[].
Perhaps super-smart optimizers might more or less inline
false-virtual-calls at runtime, but such optimization were not
effectively possible in 1998 (and are still not perfect today).
Furthermore, it doesn't solve the space overhead problem.
C++ is a language efficient & implementable right now... It was created
for that purpose from the start : Be a language useful right now (in
the tradition of the C language).

May 30 '06 #14
mangesh wrote:
what should be done so that void m() can not be overriden in D


You should not want to try.

Ask yourself if you _really_ know for an absolute _fact_ that nobody will
_ever_ want to override your virtual. Only a control freak would think
they know that. Overriding is already useful for your method, because you
yourself already overrode it. Do not presume to know better than your
clients what they should want to do with your class.

As a major example of this hubris, folks whose job is teaching teams to
retrofit unit tests onto test-free systems must often write Mock Objects.
And they discover their 3rd party, closed-source libraries are full of
objects that cannot be cleanly mocked, because their authors, with
god-complexes, decided that nobody could override their most useful
methods. These authors did not deign to consider an entire category of
usage - as base classes for Mock Objects.

http://blogs.objectmentor.com/Articl...DeprecateFinal

http://tinyurl.com/rups8

--
Phlip
May 30 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anthony Martin | last post by:
I've been reading the Java Language Specification, and in Chapter 16 there's an interesting topic called Definite Assignment. http://tinyurl.com/3fqk8 I'm wondering about the idea of "Deferred...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
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...
14
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...
5
by: Hitesh Patel | last post by:
hi friends, read following code and my problem. class B { private: public: void fun1(void) {
3
by: no1zson | last post by:
I have been working on this application for weeks now, it is almost finished, but I am getting errors that I am unable to work through. Can someone look at my code and see if anything stands out...
3
by: madsornomads | last post by:
Hi all, I have a problem with reading from a Java server after I have written to it - it just hangs. It works fine if I just write to the server and not try to write. I have read the HOWTO on...
0
by: kuguy | last post by:
Hi all, I'm new to the forums, so I hope this isn't in the wrong place... I have that "Software caused connection abort: socket write error" exception error that i've never meet before. ...
8
blazedaces
by: blazedaces | last post by:
So I have a program below which writes an excel file with multiple sheets based on inputs of sheet names, data, cell types, etc. It uses Apache POI, which is currently the only thing I found...
0
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,...
0
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...
0
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.