Sealed Classes in C++

B

Bharat Karia

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
 
I

Ioannis Vranos

Bharat said:
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 draft 1.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.
 
I

Ioannis Vranos

Ioannis said:
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.
 
S

Sharad Kala

Bharat Karia said:
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
 
D

David Hilsee

Sharad Kala said:
Bharat Karia said:
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?")
 
Joined
Dec 9, 2009
Messages
1
Reaction score
0
Sealing a Class in C++

Dear Bharat find the Below Code

#include<iostream.h>
#include<conio.h>

class SealedClass
{
private:
SealedClass(){}
public:
int A;
static SealedClass GetInstance()
{
SealedClass sealedclassObj;
return sealedclassObj;
}
};

class Inherited : SealedClass //will produce an Error SealedClass::SealedClass() is not accessible
{
public:
Inherited() {};
};

void main()
{
int i;
clrscr();
SealedClass SC = SealedClass::GetInstance(); //This way you can create the object of Sealed Class
SC.A = 10;
SealedClass SC1 = SealedClass::GetInstance();
SC1.A = 20;

cout << "SC.A = " << SC.A << "\n";
cout << "SC1.A = " << SC1.A;
getch();
}

Its is very Sinple & Self Explanatory.....Happy Coding....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top