473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

final class

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:
C()
{
printf("in private constructor of C\n");
}
};

class E : public C
{
public: E()
{
}
};

E obj; // causes compile time error saying C() can't be
accessed.

However this just makes class E abstract and doesn't really stop
inheritance from class C. In fact, there isn't any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?

Thanks in advance!!!
Dec 9 '07 #1
14 2920
On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:
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:
C()
{
printf("in private constructor of C\n");
}

};

class E : public C
{
public: E()
{
}

};

E obj; // causes compile time error saying C() can't be
accessed.

However this just makes class E abstract and doesn't really stop
inheritance from class C. In fact, there isn't any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?
And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?
Dec 9 '07 #2
On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:
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:
C()
{
printf("in private constructor of C\n");
}
};
class E : public C
{
public: E()
{
}
};
E obj; // causes compile time error saying C() can't be
accessed.
However this just makes class E abstract and doesn't really stop
inheritance from class C. In fact, there isn't any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?

And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?
Yes, to an extent an object can't be created. But i was expecting the
compiler to
give an error just like how final works with java...
Dec 9 '07 #3
On 2007-12-09 07:33, Rahul wrote:
On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
>On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:
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:
C()
{
printf("in private constructor of C\n");
}
};
class E : public C
{
public: E()
{
}
};
E obj; // causes compile time error saying C() can't be
accessed.
However this just makes class E abstract and doesn't really stop
inheritance from class C. In fact, there isn't any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?

And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?

Yes, to an extent an object can't be created. But i was expecting the
compiler to
give an error just like how final works with java...
No, you will only get the error when you try to instantiate.A better
question though, what is the use of a final mechanism? To me it seems a
bit short-sighted to assume that there will never be a situation where
one would need to inherit from the class.

--
Erik Wikström
Dec 9 '07 #4
On Dec 9, 9:33 am, Rahul <sam_...@yahoo.co.inwrote:
On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:


On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:
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:
C()
{
printf("in private constructor of C\n");
}
};
class E : public C
{
public: E()
{
}
};
E obj; // causes compile time error saying C() can't be
accessed.
However this just makes class E abstract and doesn't really stop
inheritance from class C. In fact, there isn't any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?
And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?

Yes, to an extent an object can't be created. But i was expecting the
compiler to
give an error just like how final works with java...
In C++ nothing is final.why should a class be not extensible?
I even cannot see why currently intrinsic types cannot be derived.

regards,
FM.
Dec 9 '07 #5
On Dec 9, 4:32 pm, terminator <farid.mehr...@gmail.comwrote:
On Dec 9, 9:33 am, Rahul <sam_...@yahoo.co.inwrote:


On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:
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:
C()
{
printf("in private constructor of C\n");
}
};
class E : public C
{
public: E()
{
}
};
E obj; // causes compile time error saying C() can't be
accessed.
However this just makes class E abstract and doesn't really stop
inheritance from class C. In fact, there isn't any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?
And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?
Yes, to an extent an object can't be created. But i was expecting the
compiler to
give an error just like how final works with java...

In C++ nothing is final.why should a class be not extensible?
I even cannot see why currently intrinsic types cannot be derived.
In C++, nothing is final as such (unless the constructors are made
private - and getting an object of the class is via some friend
factory class or exposes a public getter for new instances). But it
does not mean that everything is extensible.

Extensible are classes whose current design is capable of or "hints"
at possible future extensions. If none of the member functions are
virtual (in which case the destructor won't be made virtual as well),
it tells that there is no functionality to be "overridden". If the
destructor isn't virtual (irrespective of if it has virtual functions
or not), it tells that there is nothing to be "extended". One could
argue about safely using classes derived from classes not having
virtual destructors, but that is different. That is not extension.
That is just a way to save some typing. Inheritance does help in
saving some typing but that is not the real goal: runtime
polymorphism.

I wonder why you would want to derive from intrinsic types? The usual
way to build upon them would be via composition.
Dec 9 '07 #6
On Dec 9, 11:57 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-12-09 07:33, Rahul wrote:
On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:
A better question though, what is the use of a final
mechanism? To me it seems a bit short-sighted to assume that
there will never be a situation where one would need to
inherit from the class.
In particular, there are meta-programming techniques which
depend on being able to derive from a class (without ever
created instances of the derived class).

What might be useful is some means of declaring that a specific
function is the final override; this is important for
programming by contract. In practice, however, I've not found
any real cases of abuse, so it's more a theoretical benefit than
a real one.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 9 '07 #7
Rahul wrote:
I was searching for final class in c++
I have never understood the meaningfulness of "final" classes from an
object-oriented design point of view.

For what purpose, from OOD PoV, should one forbid inheritance from a
certain class?
Dec 10 '07 #8
On 2007-12-10 05:54:30 -0500, Juha Nieminen <no****@thanks.invalidsaid:
Rahul wrote:
>I was searching for final class in c++

I have never understood the meaningfulness of "final" classes from an
object-oriented design point of view.

For what purpose, from OOD PoV, should one forbid inheritance from a
certain class?
Java has them. What more do you need?

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 10 '07 #9
I apologize, guys. C++ has final class. I won't spend time describing
how it works, I hope your knowledge is enough to understand source
code. Check it out -
http://www.boost-consulting.com/vaul...20Programming&
Dec 10 '07 #10
Pete Becker wrote:
On 2007-12-10 05:54:30 -0500, Juha Nieminen <no****@thanks.invalidsaid:
>Rahul wrote:
>>I was searching for final class in c++

I have never understood the meaningfulness of "final" classes from an
object-oriented design point of view.

For what purpose, from OOD PoV, should one forbid inheritance from a
certain class?

Java has them. What more do you need?
That didn't answer my question.
Dec 10 '07 #11
On Dec 10, 5:54 am, Juha Nieminen <nos...@thanks.invalidwrote:
Rahul wrote:
I was searching for final class in c++

I have never understood the meaningfulness of "final" classes from an
object-oriented design point of view.

For what purpose, from OOD PoV, should one forbid inheritance from a
certain class?
It is just a simple way to allow control over a class and avoid
possibly anomalous behavior. Java specific thing is also performance.
I agree that from OOD PoV it is not needed, however, when you write
libraries (frameworks), you probably want to protect stupid users from
incorrect usage of your library. In such a way I would rather prevent
inheritance from standard STL containers, for example. That's it.
Dec 10 '07 #12
Vl*****************@gmail.com wrote:
On Dec 10, 5:54 am, Juha Nieminen <nos...@thanks.invalidwrote:
>Rahul wrote:
I was searching for final class in c++

I have never understood the meaningfulness of "final" classes from an
object-oriented design point of view.

For what purpose, from OOD PoV, should one forbid inheritance from a
certain class?

It is just a simple way to allow control over a class and avoid
possibly anomalous behavior. Java specific thing is also performance.
I agree that from OOD PoV it is not needed, however, when you write
libraries (frameworks), you probably want to protect stupid users from
incorrect usage of your library. In such a way I would rather prevent
inheritance from standard STL containers, for example. That's it.
Inheriting from standard containers can lead to some surprises. However,
there are times when it is perfectly sound to do so. This is not to say
that making a class final is never a good idea; but it is very very hard to
predict all possible legitimate uses of a class. Limiting usage by
artificially tying down the hands of client code programmers is rarely ever
a good idea. In particular, making standard containers final classes would
not be good.
Best

Kai-Uwe Bux
Dec 10 '07 #13
On Dec 10, 8:35 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Vladislav.Lazare...@gmail.com wrote:
On Dec 10, 5:54 am, Juha Nieminen <nos...@thanks.invalidwrote:
Rahul wrote:
I was searching for final class in c++
I have never understood the meaningfulness of "final" classes from an
object-oriented design point of view.
For what purpose, from OOD PoV, should one forbid inheritance from a
certain class?
It is just a simple way to allow control over a class and avoid
possibly anomalous behavior. Java specific thing is also performance.
I agree that from OOD PoV it is not needed, however, when you write
libraries (frameworks), you probably want to protect stupid users from
incorrect usage of your library. In such a way I would rather prevent
inheritance from standard STL containers, for example. That's it.

Inheriting from standard containers can lead to some surprises. However,
there are times when it is perfectly sound to do so. This is not to say
that making a class final is never a good idea; but it is very very hard to
predict all possible legitimate uses of a class. Limiting usage by
artificially tying down the hands of client code programmers is rarely ever
a good idea. In particular, making standard containers final classes would
not be good.

Best

Kai-Uwe Bux
I agree. It was just an example. Want you to do final class or not,
having ability to do it is good. I love C++ because it is possible to
do everything. Why not final class?
Dec 10 '07 #14
Vl*****************@gmail.com wrote:
On 10 ???, 22:23, "Alf P. Steinbach" <al...@start.nowrote:
>* Vladislav.Lazare...@gmail.com:
I love C++ because it is possible to
do everything. Why not final class?

It's often a good idea to check the FAQ before posting.

<url:http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11>
>>
[snip]
>
Alf, looks like we misunderstood each other. I know it is possible.
Moreover, solution described in FAQ is not general. You should always
write base class from scratch, right? The code I have posted to Boost
Vault is general solution. The only you should do is inherit from
"nonderivable" class, that's it. The only thing user should remember -
not all compilers, probably, supports template friends.
If by "template friends" you should mean something like

template < typename T >
class dummy {
friend class T;
...
};

then no standard compliant compiler will support template friends.

(Now, of course, I am guessing here from context; but I presume that you
want to use templated inheritance for implementing a final<template that
does not require the user to manually use virtual inheritance. I think
that, at least as of the current standard, [7.1.5.3/2] blocks any attempt
in that direction.)

You can read about C++ "final" in Boost mailing list:
http://lists.boost.org/Archives/boos.../05/122446.php

Best

Kai-Uwe Bux
Dec 10 '07 #15

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

Similar topics

2
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...
2
by: BobReynolds | last post by:
We use Log4j for all our logging, I want to know why we have to do the following in EVERY class final private static org.apache.log4j.Logger log = LoggerUtil.getLogger(thisClassName.class); ...
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...
10
by: Bezalel Bareli | last post by:
I know I have seen some threads on the subject long time ago and it was using a virtual base class ... in short, what is the nicest way to implement the Java final class in c++ Thanks.
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: silverburgh.meryl | last post by:
I am trying to convert this code from java to c++: public final class Type { public static final int DEFAULT = 1; private static int index = 2; public static final int COLUMN1 = (int)...
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...
1
by: Rajib | last post by:
Not that this serves any real purpose, but gcc allows me to do some hack like this: class hide_A { public: class A { public: virtual int final() { return 42; } };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.