473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to prevent a class from being inheritable, in C++

Hi,

I didn't know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya
Jul 22 '05
11 1615

"Saumya" <sa**********@y ahoo.com> wrote in message
news:e9******** *************** ***@posting.goo gle.com...
Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.

U mean how to simulate final java class concept in c++.....here is
program.I guess its similar to creating singleton pattern

// Make ctor private so that class cannot be inheriated

//Use static member function to create object of its own which will return
ptr to same class obj

#include <iostream.h>

class X{

private :

int x;

X(); // private ctor

public :

static X* getMePrivateObj ect(); // this will help me creating handle rather
for normal object

};
X::X(){

cout<<"in constructor"<<e ndl;

x=0;

};
X* X::getMePrivate Object(){

X *pMyP = new X();

return pMyP;

};

/*class Y : public X //cannot do this as X has private ctor

{

public:

Y(){}

void print(){ cout<<"Display me if u can";}

};

*/

void main(void){

X *pMyP = X::getMePrivate Object();

//Rest of ur code

}
Jul 22 '05 #11
Check this program....Clas s X acts like final class in java.

#include <iostream.h>
class X{
private :
int x;
X(); // private ctor
public :
static X* getMePrivateObj ect(); // this will help me creating handle rather
for normal object
};
X::X(){
cout<<"in constructor"<<e ndl;
x=0;
};
X* X::getMePrivate Object(){
X *pMyP = new X();
return pMyP;
};
/*class Y : public X //cannot do this as X has private ctor
{
public:
Y(){}
void print(){ cout<<"Display me if u can";}
};
*/
void main(void){
X *pMyP = X::getMePrivate Object();
//Rest of ur code
}
Njoy,

Datta Patil
Jul 22 '05 #12

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

Similar topics

6
3195
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
2
2293
by: Mark | last post by:
Hi all, quick question, how do I make a define a class as being inheritable only?? Thanks Mark
20
2884
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
5
2138
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
3
1694
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that I can prevent the control from being designed by making it not the first class in the code file, but is there another more 'official' way to do this via attributes.
13
3159
by: Rob Meade | last post by:
Hi all, I have written a small ProperCase function which I would like to make available to our team at work through our common class library. A colleague mentioned that I could write a new class which derived from the String class, add the function there and then we would all use the new class which contained my function and all of the others etc....sounded relatively straight forward...
16
4245
by: Richard Brown | last post by:
Ok, now I am truely going nuts... probably why I didn't use the Class Builder in VB6 extensively. But, being the 'proper programmer' that I should, I'm trying to bite the bullet and build classes for each of my 'entities' that operate within the program, ie, passenger, vehicle, etc. Then seperate UI classes that link to these. (Partially, because eventually several add-ons will be able to reuse the code in the data classes). My...
5
3400
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface.". I am not interested in serializing any member from the base class only the properties in the derived class. How can I prevent the entire base class from being...
5
3169
by: Jon Slaughter | last post by:
Why did microsoft seal these classes? I would like to add coordinate information to these classes but I can't derive from them ;/ It makes me wonder why microsft choose to prevent anyone from deriving from many classes. I can simply do class MyBitmap { public Bitmap B;
0
8946
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
8774
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,...
1
9235
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
6031
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
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.