473,756 Members | 9,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Don't want to make my class as base class

Hi, I just don't want anybody derive a class from my class. For
example:

///////////////////////
//MY CODE

class MyClass {
....
};
///////////////////////
/////////////////////
// Somebody's code

class HisClass : public MyClass { // This line should give a compile
time error
....
}
////////////////////

How can I make MyClass not to be a base class of other

Thanks,
Nataraj

Jul 29 '05 #1
4 1841
* Nataraj M:
Hi, I just don't want anybody derive a class from my class. For
example:

///////////////////////
//MY CODE

class MyClass {
...
};
///////////////////////
/////////////////////
// Somebody's code

class HisClass : public MyClass { // This line should give a compile
time error
...
}
////////////////////

How can I make MyClass not to be a base class of other


The FAQ is at <url: http://www.parashift.c om/c++-faq-lite/>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 29 '05 #2
Alf Steinbach: Here's an excerpt from www.artimus.com which may help
you.

Nguyen Minh Huy

How to create final class in C++? Posted: Jun 18, 2005 6:02 AM
Re: How to create final class in C++? Posted: Jun 25, 2005 12:51 AM
Reply

responding to Nguyen

In C++ there is no keyword (final) to declare a class as
non-inheritable as in Java. But then C++ has its own features which you
may exploit to get the same behaviour.

Bsically it uses concepts of private constructor and friend class.

Idea is if you derive(virtual public) a class from another class having
private constructors, you cannot create an object of derived class.

To avoid this you need to declare derived (Final class) class as friend
of base class. So that now if some one tries to inherit from this Final
class, compilation gives error as this class cannot call constructor of
its super class i.e. Final class's super class i.e. base class that has
private constructor.

Hope this helps.

regards,
Shashank

Alf P. Steinbach wrote:
* Nataraj M:
Hi, I just don't want anybody derive a class from my class. For
example:

///////////////////////
//MY CODE

class MyClass {
...
};
///////////////////////
/////////////////////
// Somebody's code

class HisClass : public MyClass { // This line should give a compile
time error
...
}
////////////////////

How can I make MyClass not to be a base class of other


The FAQ is at <url: http://www.parashift.c om/c++-faq-lite/>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jul 29 '05 #3
Frank Chang wrote:

Alf Steinbach: Here's an excerpt from www.artimus.com which may help
you.

I don't think that Alf needs that help.
Bsically it uses concepts of private constructor and friend class.


And this differs exactly in which way from what is described in the FAQ?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 29 '05 #4
Karl, This post was intended for Nataraj M not Alf Steinbach. If one
goes to the link you are referring to, then one has to do a search to
find this topic. I thought I would save Nataraj M the time to do this
search. Anyway, I think we are quibbling about something that has
nothing to do with C++. Thank you for your moderator comments.

Karl Heinz Buchegger wrote:
Frank Chang wrote:

Alf Steinbach: Here's an excerpt from www.artimus.com which may help
you.


I don't think that Alf needs that help.
Bsically it uses concepts of private constructor and friend class.


And this differs exactly in which way from what is described in the FAQ?

--
Karl Heinz Buchegger
kb******@gascad .at


Jul 29 '05 #5

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

Similar topics

6
1301
by: Daniel Billingsley | last post by:
I understand the conditions where you have to use base., but does it make any sense to use it elsewhere to make clear to the reader that the method or property you refer to comes from the base class? Or, would you find such code insulting or clutter since you're smart enough to see that the class is inherited?
3
1436
by: Francois | last post by:
Hi, I am trying to extend a .NET class for overriding some methods. The class that I try to override is : System.Web.UI.WebControls.CheckBox. Its full definition is : public class CheckBox : WebControl, IPostBackDataHandler (notice the interface that is implemented) I want to override 3 methods belonging to that class:
1
1410
by: Geoff Hilyard | last post by:
Using VS.net 2003 / Managed C++ I have created a base object (public __gc CBaseObject) that everything in my project inherits from (Similar to how everything really inherits system::Object). I have a Heap class that I have made into a DLL. In this dll, I include CBaseObject (I have to dynamic cast to it for access to some of its functions). This is made into a dll for use in several other projects. However, when I include (I guess...
5
1351
by: z f | last post by:
the HTML designer for aspx pages don't work is the page inherits from a custom pageBase. any solution? TIA.
6
5125
by: Jim Langston | last post by:
Short version: class MyClass { friend class MyOtherClass::MySubClass; // Doesn't work }; class MyOtherClass { class MySubClass {
9
3178
by: Johnny Jörgensen | last post by:
Hi y'all I've got a custom control that is inherited from a normal TextBox. To that, I've added a new property that I call "Units", Which is a collection class containing members of a "Unit" class. When I put my Textbox on the form, I can access the "Units" property from the IDE's property grid, use the collection designer and add new "Unit" members to the class.
31
1919
by: Jo | last post by:
class A { public: char text_a; A() { *text_a=0; } ~A() {} }; //-----------------------------------------------------------------------------
3
1480
by: Immortal Nephi | last post by:
The rule of inheritance states that you define from top to bottom. Sometimes, you want to define base class and set reference from dervied class to base class, but you violate the rule. Here is an example of my code below. class A {}; class B : public A {}; int main(void) {
1
6712
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
9117
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
9894
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
9541
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
8542
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
7078
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
6390
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
5156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3651
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
3141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.