473,698 Members | 2,426 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 2352
"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
2730
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
5156
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
3471
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
2069
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
2528
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
8678
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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,...
1
8899
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,...
1
6525
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
5861
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
4371
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
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.