473,734 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a way to disallow subclassing

Is there way to write a C++ class so that it cannot be subclassed?
Jul 19 '05 #1
5 2355
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** **@posting.goog le.com...
Is there way to write a C++ class so that it cannot be subclassed?


There is, but it's unfortunately excessively complicated,
making it inconvenient to use.
It can be done by using virtual inheritance:

// class with private constructor and destructor
class Locker {
friend NotToBeSubclass ed;
Locker() {};
~Locker() {};
};

class NotToBeSubclass ed : virtual Locker
{
//... whatever
};

The trick is: a most-derived class must call the constructor
of all virtual base classes of its parents. But the private
constructor of 'Locker' is only accessible to the
'NotToBeSubclas sed' class, which therefore cannot
be derived from.
hth, Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #2
Ivan Vecerina wrote:
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** **@posting.goog le.com...
Is there way to write a C++ class so that it cannot be subclassed?

There is, but it's unfortunately excessively complicated,
making it inconvenient to use.
It can be done by using virtual inheritance:

i'm not following you, why is required the virtual inheritance?

-----[ Domenico Andreoli, aka cavok
--[ http://filibusta.crema.unimi.it/~cavok/gpgkey.asc
---[ 3A0F 2F80 F79C 678A 8936 4FEE 0677 9033 A20E BC50

Jul 19 '05 #3
"Domenico Andreoli" <ca***@freemail .it> wrote in message
news:VG******** **************@ twister1.libero .it...
Ivan Vecerina wrote:
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** **@posting.goog le.com...
Is there way to write a C++ class so that it cannot be subclassed?

There is, but it's unfortunately excessively complicated,
making it inconvenient to use.
It can be done by using virtual inheritance:

i'm not following you, why is required the virtual inheritance?


The most derived subclass is always responsible for initializing
a virtual base class. A non-virtual base class does not affect
subclasses.
Regards, Ivan
--
http://ivan.vecerina.com


Jul 19 '05 #4
mj*******@hotma il.com (Mohammad) wrote in message news:<3d******* *************** ***@posting.goo gle.com>...
Is there way to write a C++ class so that it cannot be subclassed?


It's easy: declare all your class constructors private!
Jul 19 '05 #5

"Bobo" <pi***@correo.n u> wrote in message
news:74******** *************** ***@posting.goo gle.com...
mj*******@hotma il.com (Mohammad) wrote in message

news:<3d******* *************** ***@posting.goo gle.com>...
Is there way to write a C++ class so that it cannot be subclassed?


It's easy: declare all your class constructors private!


of course then you can't construct one either. useful for singletons
though. or, you could have a static method that constructed them for you
and returned a pointer
Jul 19 '05 #6

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

Similar topics

6
2731
by: WhiteRavenEye | last post by:
Why can't I subclass any window except mine in VB? Do I have to write dll for this? I've tried to subclass it with SetWindowLong but without success... Does anyone know how to subclass window ANY window in VB? Thanks...
2
5160
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this class which is supposed to be a representation of a genome: class Genome(list): def __init__(self): list.__init__(self) self = ....
11
3476
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
16
2076
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do it with inheritance, a kind of "dynamic subclassing", without subclassing the class, only this instance "b" ! In fact, i've got my instance "b", and another class "MoreMethods"
5
2531
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
8776
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8186
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
6735
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
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.