473,586 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2967
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.padma n...@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.padma n...@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.padma n...@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.padma n...@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.padma n...@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 objektorientier ter Datenverarbeitu ng
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

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

Similar topics

2
5302
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...
2
14796
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); If all by Classes extend of other classes, and everything needs logger, why dont we just go protected org.apache.log4j.Logger log =
0
2337
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
23115
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
10
5103
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
4974
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a...
1
8607
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) Math.pow(2, index++); public static final int COLUMN2 = (int) Math.pow(2, index++); public static final int COLUMN3 = (int) Math.pow(2, index++); public...
3
1738
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 that I might try correcting. First, I am getting Inventory2.java:187: actionPerformed(java.awt.event.ActionEvent) in cannot implement...
1
1689
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
7839
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...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
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...

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.