473,666 Members | 2,617 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 5109

"Bezalel Bareli" <ba************ @optonline.net> wrote in message
news:9f******** *************** **@posting.goog le.com...
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.goog le.com...
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<typena me 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.goo gle.com>...
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*****@borgha rdtConsulting.n et> wrote in message
news:QUhrc.5532 55$Ig.357552@pd 7tw2no...
Hi. try this:

template<typena me 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

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

Similar topics

2
5305
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...
2
14800
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
2341
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
23122
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
6
4986
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 PDF. It works fine so far....
1
8611
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 static final int COLUMN4 = (int) Math.pow(2, index++);
3
1742
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 actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener; attempting to assign weaker access...
14
2975
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:
1
1694
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; } };
1
8561
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
8645
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5672
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
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
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
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
1778
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.