473,509 Members | 7,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ Final class ?

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.
Jul 22 '05 #1
10 5095

"Bezalel Bareli" <ba************@optonline.net> wrote in message
news:9f*************************@posting.google.co m...
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.


http://www.research.att.com/~bs/bs_f...#no-derivation

john
Jul 22 '05 #2
Thanks John; I was actually thinking on that path, however, I am
trying to templatize the idea. Do you think it is template legal; I
tried the exercise and failed compiling but I will read my template
book again.
Jul 22 '05 #3

"Bezalel Bareli" <ba************@optonline.net> wrote in message
news:9f*************************@posting.google.co m...
Thanks John; I was actually thinking on that path, however, I am
trying to templatize the idea. Do you think it is template legal; I
tried the exercise and failed compiling but I will read my template
book again.


I couldn't get it to work either. I would say it cannot be templatized
because of the problem of trying to grant friendship to the derived class,
but I could be wrong.

john
Jul 22 '05 #4
"John Harrison" wrote:
I couldn't get it to work either. I would say it cannot be templatized
because of the problem of trying to grant friendship to the derived class,
but I could be wrong.


I did not read the book, so I am probably missing something, but what
need is there for that "friend" line*?
Jul 22 '05 #5

"Marc" <Ma***********@Loria.Fr> wrote in message
news:c8***********@nef.ens.fr...
"John Harrison" wrote:
I couldn't get it to work either. I would say it cannot be templatized
because of the problem of trying to grant friendship to the derived class, but I could be wrong.


I did not read the book, so I am probably missing something, but what
need is there for that "friend" line ?


The point is to prevent derivation from Usable. Usable can construct
Usable_lock because Usable_lock has granted it friendship. Any class derived
from Usable will not be a friend of Usable_lock and so will not be able to
construct Usable_lock and therefore, as Usable_lock is a virtual base, will
not be able to be constructed at all.

john
Jul 22 '05 #6
Hi. try this:

template<typename T>
class MakeFinal
{
private:
MakeFinal() {std::cout << "MakeFinal Hello World" << std::endl;};
~MakeFinal() {std::cout << "MakeFinal Goodbye World" << std::endl;};
friend T;
};

class Final : virtual public MakeFinal<Final>
{
public:
Final() {std::cout << "Final Hello World" << std::endl;};
~Final() {std::cout << "Final Goodbye World" << std::endl;};
};

/* error Final not deriveable */
class Derived : public Final
{
public:
Derived() {std::cout << "Derived Hello World" << std::endl;};
~Derived() {std::cout << "Derived Goodbye World" << std::endl;};
};
Jul 22 '05 #7
ba************@optonline.net (Bezalel Bareli) wrote in message news:<9f*************************@posting.google.c om>...
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++


Why not make the constructors private and write a public static factory method?

-Luther
Jul 22 '05 #8
>Why not make the constructors private and write a public static factory
method?


Making constructors private for the purpose of preventing derivation has the
possibly unwanted consequence of placing restrictions on how/where objects are
instantiated.

The virtual-base class based solution prevents derivation without imposing such
restrictions allowing the "final" class to be used in a natural manner.


Jul 22 '05 #9

"Michael D. Borghardt" <mi*****@borghardtConsulting.net> wrote in message
news:QUhrc.553255$Ig.357552@pd7tw2no...
Hi. try this:

template<typename T>
class MakeFinal
{
private:
MakeFinal() {std::cout << "MakeFinal Hello World" << std::endl;};
~MakeFinal() {std::cout << "MakeFinal Goodbye World" << std::endl;};
friend T;
};

class Final : virtual public MakeFinal<Final>
{
public:
Final() {std::cout << "Final Hello World" << std::endl;};
~Final() {std::cout << "Final Goodbye World" << std::endl;};
};


I just did, after adding 'int main() { Final f; }' I get

main.cpp(26) : error C2248: 'MakeFinal<T>::__ctor' : cannot access private
member declared in class 'MakeFinal<T>'
with
[
T=Final
]
and
[
T=Final
]
and
[
T=Final
]

on MSVC++ 7.1. On gcc 3.3.1 I get

error: template parameters cannot be friends

On Comeau C++ I get

line 26: error: "MakeFinal<T>::MakeFinal() [with T=Final]" is
inaccessible
Derived() {std::cout << "Derived Hello World" << std::endl;};

What did you get on your compiler?

john
Jul 22 '05 #10
Hi.

This compiles under MSVC 7.0 and the free MSVC 7.1

Also compiler under gcc 2.96

works on Comeau non-strict

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:non-strict warnings C++

compiler error strict

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 8: error: omission of "class" is nonstandard
friend T;
^

1 error detected in the compilation of "ComeauTest.c".

If I use friend typename T in strict mode

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 8: error: a class or namespace qualified name is
required
friend typename T;
^

"ComeauTest.c", line 13: error: "MakeFinal<T>::MakeFinal() [with T=Final]"
is
inaccessible
Final() {std::cout << "Final Hello World" << std::endl;};
^

"ComeauTest.c", line 13: error: "MakeFinal<T>::~MakeFinal() [with T=Final]"
is
inaccessible
Final() {std::cout << "Final Hello World" << std::endl;};
^

"ComeauTest.c", line 14: error: "MakeFinal<T>::~MakeFinal() [with T=Final]"
is
inaccessible
~Final() {std::cout << "Final Goodbye World" << std::endl;};
^

4 errors detected in the compilation of "ComeauTest.c".
Below is the code I used#include <iostream>
template<typename T>
class MakeFinal
{
private:
MakeFinal() {std::cout << "MakeFinal Hello World" << std::endl;};
~MakeFinal() {std::cout << "MakeFinal Goodbye World" << std::endl;};
friend T;
};
class Final : virtual public MakeFinal<Final>
{
public:
Final() {std::cout << "Final Hello World" << std::endl;};
~Final() {std::cout << "Final Goodbye World" << std::endl;};
};
/*
class Derived : public Final
{
public:
Derived() {std::cout << "Derived Hello World" << std::endl;};
~Derived() {std::cout << "Derived Goodbye World" << std::endl;};
};
*/
int main()
{
Final f;
// Derived *d = new Derived();
// delete d;
return 1;
}
Jul 22 '05 #11

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

Similar topics

2
5298
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
14787
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
2329
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
23104
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...
6
4961
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
8592
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
1735
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...
14
2935
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...
1
1682
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
7237
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
7137
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
7416
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
7073
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
7506
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...
0
5656
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,...
0
3218
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...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
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...

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.