By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

How to create a Leaf class in C++

P: 93
If I want a class to be made as leaf class so that it cannot be inherited any
more. How can I do this.

I had heard that there is a keyword final, in Java which makes it as leaf
class, but dont know what is its equivalent in C++.

for example, make class C as leaf class.
Expand|Select|Wrap|Line Numbers
  1.         class A
  2.         {
  3.         };
  4.         class B: public A
  5.         {
  6.         };
  7.         class C: public B   // add something to make C as leaf
  8.         {
  9.         };
  10.         class D: public C // this should give error
  11.         {
  12.         };
  13.  
Regards,
Girish.
Jun 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: 55
Why u want o protect your class from being inherit ? Software reuse is a important concept in modern programming environment and save us a lot of time.

Reconsider your class design, if you want to do so.
Jun 12 '07 #2

P: 55
Declare the class constructors as private. I hope this help others.
Jun 12 '07 #3

P: 93
Why u want o protect your class from being inherit ? Software reuse is a important concept in modern programming environment and save us a lot of time.

Reconsider your class design, if you want to do so.
The Reason of protecting is if we are giving closed source product
to a company (ofcourse not free of cost)
with shared objects and even prototypes of functions without giving
the implimentation part then, as a security perspective it is needed
make the leaf classes in our application as final classes so that
nobody else can inherit the same.

Did you get what I mean ?????????????
Jun 12 '07 #4

gpraghuram
Expert 100+
P: 1,275
HI,
I think you can make the last class singleton class
Expand|Select|Wrap|Line Numbers
  1. class A
  2.         {
  3.         };
  4.         class B: public A
  5.         {
  6.         };
  7.         class C: public B   // add something to make C as leaf
  8.         {
  9. //Make this as singleton class
  10.       private:
  11.         C()
  12.         {}
  13.        ~C(){}
  14. public:
  15.    static C& instance(int x=0,int y=0) 
  16.    {
  17.     static C singleton;
  18.     return singleton;
  19.   }
  20.         };
  21.         class D: public C // this should give error
  22.         {
  23. //If u make C as singleton hen this will surely give error.
  24.         };
  25.  
Post whether this is what u expected.?

Raghuram
Jun 12 '07 #5

P: 93
HI,
I think you can make the last class singleton class
Expand|Select|Wrap|Line Numbers
  1. class A
  2.         {
  3.         };
  4.         class B: public A
  5.         {
  6.         };
  7.         class C: public B   // add something to make C as leaf
  8.         {
  9. //Make this as singleton class
  10.       private:
  11.         C()
  12.         {}
  13.        ~C(){}
  14. public:
  15.    static C& instance(int x=0,int y=0) 
  16.    {
  17.     static C singleton;
  18.     return singleton;
  19.   }
  20.         };
  21.         class D: public C // this should give error
  22.         {
  23. //If u make C as singleton hen this will surely give error.
  24.         };
  25.  
Post whether this is what u expected.?

Raghuram
Why did you make this "singleton" instance as static and more over
the "singleton" which is returned is not a pointer type, then it calls copy
constructor to assign to caller and more over caller cannot be instantiated
since the constructor is private.
Can you just show me that 3 to 4 lines of code in main for creating instance
of class C.

More over I may wish to have multiple objects for C not just
one object.

Regards,
Girish.
Jun 12 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
This problem:
The Reason of protecting is if we are giving closed source product
to a company (ofcourse not free of cost)
with shared objects and even prototypes of functions without giving
the implimentation part then, as a security perspective it is needed
make the leaf classes in our application as final classes so that
nobody else can inherit the same.
is not solved by making class C final.

It is solved by not exposing the class C definition to your customers. The header you use to distrubute your product should have only a class C pointer.

The actual code should be in library form. Customers never get source.

Your docset would define what methods you could call using a class C pointer.
Jun 12 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.