473,883 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create class which can't be inherited

KP
How can we create class which other class can't be inherited or we can say
how to protect class from inheritance ? ( e.g. final class in java, if a
class is final in java, then this class can't be inherited)

Ketan


Mar 23 '06 #1
8 2867
Hello is this Ajay who posted this message

Mar 23 '06 #2
KP
No, this is not ajay. I don't know if this question has been already asked
in this group. Do you know any solution of this?
<pa******@gmail .com> wrote in message
news:11******** **************@ e56g2000cwe.goo glegroups.com.. .
Hello is this Ajay who posted this message

Mar 23 '06 #3

KP wrote:
How can we create class which other class can't be inherited or we can say
how to protect class from inheritance ? ( e.g. final class in java, if a
class is final in java, then this class can't be inherited)


This is in the FAQ
http://www.parashift.com/c++-faq-lit...html#faq-23.11

Gavin Deane

Mar 23 '06 #4
On 23 Mar 2006 01:39:24 -0800 "Gavin Deane" <de*********@ho tmail.com>
waved a wand and this message magically appeared:
How can we create class which other class can't be inherited or we can say
how to protect class from inheritance ? ( e.g. final class in java, if a
class is final in java, then this class can't be inherited)


This is in the FAQ
http://www.parashift.com/c++-faq-lit...html#faq-23.11


I tried writing a class that can't be inherited but can be used but
couldn't get it to work; perhaps I'm not understanding this properly.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 23 '06 #5

Alex Buell wrote:
On 23 Mar 2006 01:39:24 -0800 "Gavin Deane" <de*********@ho tmail.com>
waved a wand and this message magically appeared:
How can we create class which other class can't be inherited or we can say
how to protect class from inheritance ? ( e.g. final class in java, if a
class is final in java, then this class can't be inherited)


This is in the FAQ
http://www.parashift.com/c++-faq-lit...html#faq-23.11


I tried writing a class that can't be inherited but can be used but
couldn't get it to work; perhaps I'm not understanding this properly.


I think that FAQ has been around for some time, and I've seen it
referred to before. I would expect any mistakes in it to have been
caught by now. I can't say for definite from my own experience, having
never needed to write a "final" class.

Post a minimal complete program
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
that uses the techniques in the FAQ but doesn't work as you'd like and
you should get some help.

Gavin Deane

Mar 23 '06 #6
On 23 Mar 2006 02:59:12 -0800 "Gavin Deane" <de*********@ho tmail.com>
waved a wand and this message magically appeared:
Post a minimal complete program
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
that uses the techniques in the FAQ but doesn't work as you'd like and
you should get some help.


I think I've got the idea:

#include <iostream>

class NotInheritable
{
public:
static NotInheritable create() { return NotInheritable( ); };

private:
NotInheritable( ) { std::cout << "NotInheritable ::NotInheritabl e
()" << std::endl; };
};
class TryToInherit : NotInheritable
{
public:
TryToInherit() { std::cout << "TryToInherit:: TryToInherit()" <<
std::endl; };
};

int main()
{
NotInheritable ni = NotInheritable: :create();
return 0;
}
--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 23 '06 #7
Here's example code for an alternative method:

class FinalClass
{
public:
static FinalClass * Create_FinalCla ss();
private:
FinalClass(cons t FinalClass&);
FinalClass& operator=(const FinalClass&);
FinalClass(){}
};

FinalClass* FinalClass::Cre ate_FinalClass( )
{
return new FinalClass();
}

int main(int argc, char* argv[])
{
FinalClass *f = FinalClass::Cre ate_FinalClass( );
delete f;
//Or safer method, use auto pointer
std::auto_ptr<F inalClass> f_safe(FinalCla ss::Create_Fina lClass());

return 0;
}

Mar 23 '06 #8

"KP" <K@P.com> skrev i meddelandet
news:dv******** **@daniel-new.mch.sbs.de. ..
No, this is not ajay. I don't know if this question has been already
asked in this group. Do you know any solution of this?


The question is WHY do you want this?

In Java there is a reason to make the class final, beacuse it allows
the code to be optimized when we know that the virtual functions
cannot be overridden by a subclass.

In C++ you can avoid this by not making the functions virtual in the
first place.
If the object is just that inhereting from your class won't work, I
recommend solution #2 from the FAQ:
State in the comments/documentation that inhereting is not allowed -
"Me and my baseball bat will visit any violators!".
Bo Persson

Mar 23 '06 #9

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

Similar topics

7
28990
by: Dominique | last post by:
Suppose I have: class A { A(int x, int y); }; and class B: public A {
8
3084
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this? this.GetType().GetMember("panel1").SetValue(xx).Left = 44;
4
3290
by: Antuane | last post by:
i'm trying to create a custom textbox class, by simply creating a new class & inheriting from the textbox class. But i don't have a UI of this class. I.e., how can i set up the default text, color for this text box - via the properties window if there is no UI. Currently i'm doing this via code & this is a headache, cuz i've got to drop this control onto a form & then check out whether my UI changes are as required. Is there a way for...
8
1350
by: Altman | last post by:
I have a class that I want many others inherited off of. I have created a variable in the parent class that I want the inherited classes to set. But I do not want to set the variable in the constructor. Ex. Class A protected myVar as string END Class Class B inherits A
6
1390
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd check the Doc and see what it says for Bitmap.FromFile. I expected it to say "(inherited from Image)". But I could find no reference to Bitmap.FromFile anywhere in the help. There were times when I looked for something and couldn't find it so I'm now
8
2030
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one of its own, maybe specifying the liquor store over on 44th Street and 5th Avenue or something. Anyway this is what we have so far: base class: "foo" |------------method: "rob_the_liquor_store()" |
14
2650
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
19
2247
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
12
2176
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
0
9942
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...
1
10847
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
10416
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
9574
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
7971
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
5797
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...
1
4612
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
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3233
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.