473,654 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sealed Classes in C++

Hi,

Is it possible to writed Sealed classes in C++ [i.e. similar to sealed
classes in C# or final classes in Java].

i.e. there is no sealed/final keyword in C++, but is it possible to achieve
the same effect? i.e. deriving from a sealed class is an error and the
compiler should flag it as such.

Thanks
Bharat Karia
Jul 22 '05 #1
5 7631
Bharat Karia wrote:
Hi,

Is it possible to writed Sealed classes in C++ [i.e. similar to sealed
classes in C# or final classes in Java].

i.e. there is no sealed/final keyword in C++, but is it possible to achieve
the same effect? i.e. deriving from a sealed class is an error and the
compiler should flag it as such.

You are talking about .NET (CLI) sealed classes, so you had better check
C++/CLI (currently draft) for that:

http://www.plumhall.com/C++-CLI%20draft%201.8.pdf
"18.1.1 Class modifiers

To accommodate the addition of sealed and abstract classes, the grammar
for class-head in the C++ Standard (§9) has been extended to include an
optional sequence of class modifiers, as follows:

class-modifiers:
class-modifiers_opt class-modifier

class-modifier:
abstract
sealed

If the same modifier appears multiple times in a class definition, the
program is ill-formed.

[Note: abstract and sealed can be used together; that is, they are not
mutually exclusive. As non-member functions are not CLS-compliant, a
substitute is to use an abstract sealed class, which can contain static
member functions. This is the utility class pattern. end note]

The abstract and sealed modifiers are discussed in §18.1.1.1 and
§18.1.1.2, respectively."

Notice here that you can define a class that is both abstract and sealed
just because the CLI (.NET) permits it, although it is of no use.

That is not possible in C#/CLI, but is possible in C++/CLI because it is
the systems programming language of .NET.

In ISO C++, there is no way to do such a thing portably.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2
Ioannis Vranos wrote:
Notice here that you can define a class that is both abstract and sealed
just because the CLI (.NET) permits it, although it is of no use.

That is not possible in C#/CLI, but is possible in C++/CLI because it is
the systems programming language of .NET.

That is also mentioned here:

http://blogs.msdn.com/branbray/archi.../07/51007.aspx


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3
Ioannis Vranos wrote:
Notice here that you can define a class that is both abstract and sealed
just because the CLI (.NET) permits it, although it is of no use.

My mistake here, as it was stated in the draft, it has its uses.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4

"Bharat Karia" <bj*****@hotmai l.com> wrote in message
Hi,

Is it possible to writed Sealed classes in C++ [i.e. similar to sealed
classes in C# or final classes in Java].
i.e. there is no sealed/final keyword in C++, but is it possible to achieve the same effect? i.e. deriving from a sealed class is an error and the
compiler should flag it as such.


OP: http://www.research.att.com/~bs/bs_faq2.html#final
Others: Is this covered in the FAQ ?

Sharad
Jul 22 '05 #5
"Sharad Kala" <no************ *****@yahoo.com > wrote in message
news:34******** *****@individua l.net...

"Bharat Karia" <bj*****@hotmai l.com> wrote in message
Hi,

Is it possible to writed Sealed classes in C++ [i.e. similar to sealed
classes in C# or final classes in Java].
i.e. there is no sealed/final keyword in C++, but is it possible to

achieve
the same effect? i.e. deriving from a sealed class is an error and the
compiler should flag it as such.


OP: http://www.research.att.com/~bs/bs_faq2.html#final
Others: Is this covered in the FAQ ?


Yes, by question 23.8 ("How can I set up my class so it won't be inherited
from?")

--
David Hilsee
Jul 22 '05 #6

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

Similar topics

6
2249
by: Julie | last post by:
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed keyword in C++, but that there are techniques to accomplish this. From what I've heard, sealed may be added to the language in the future?) I understand that there are compiler and efficiency reasons (optimizing away virtual function calls, etc.) that justify it in other languages,...
14
2929
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
13
8097
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that the code inside the constructor / desconstructor will never run? Mark
9
8403
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
10
3863
by: TJM | last post by:
Hi, Is it possible to have a method sealed and abstract at the same time? MSDN states clearly that this is not allowed for classes but it does not mention it for methods. I tried with a simple example and the compiler would not allow me to compile, however in a recent interview, I was asked this question and the interviewer claimed that this is used in certain situations! I am baffled! Thanks,
18
2947
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent definition is perfectly legal in other CLR languages. For example in C#; static class XXX
7
2341
by: Hilton | last post by:
Jon Skeet wrote: I for one wish the "sealed" keyword on a class didn't exist. Why can I not have: class NamedThread : Thread { string name; :
7
1835
by: Zytan | last post by:
I know you cannot have a sealed static class, but why not? Why must static classes be left open to inheritance? This article: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/ recommends to place DLL imports into a sealed class, with a private constructor to prevent instantiation (see Figure 1 at the very top): http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx?loc=&fig=true#fig1 Why not just make a static class to...
1
1840
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks, Mulugeta
0
8379
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
8294
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
8816
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
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7309
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...
0
4150
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
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
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.