473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 2850
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.googlegr oups.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*********@hotmail.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*********@hotmail.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*********@hotmail.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::NotInheritable
()" << 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_FinalClass();
private:
FinalClass(const FinalClass&);
FinalClass& operator=(const FinalClass&);
FinalClass(){}
};

FinalClass* FinalClass::Create_FinalClass()
{
return new FinalClass();
}

int main(int argc, char* argv[])
{
FinalClass *f = FinalClass::Create_FinalClass();
delete f;
//Or safer method, use auto pointer
std::auto_ptr<FinalClass> f_safe(FinalClass::Create_FinalClass());

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
by: Dominique | last post by:
Suppose I have: class A { A(int x, int y); }; and class B: public A {
8
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?...
4
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...
8
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...
6
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...
8
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...
14
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...
19
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...
12
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...

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.