Solved

Deriving Classes

Posted on 2003-10-28
6
475 Views
Last Modified: 2010-04-17
How can i make a class not derivable  by another classes?
0
Comment
Question by:kallzz
6 Comments
 
LVL 1

Expert Comment

by:fsign21
ID: 9633296
Which language are you using for your development?
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9634344
In Java - Declare the class as final.


ie public final ClassName



In C++ just don't make any part of the class virtual.  As long as nothing is declared as virtual, it can't be overriden by any inheriting class.



For any other language, I'm not sure.  You'll have to specify if it's not one of the above 2.
0
 
LVL 1

Expert Comment

by:fsign21
ID: 9635191
ultimatemike,
for C++ it is not true. Even if nothing declared as virtual in your class, you still can derive from this class.
You may get some problems, if you do so. But this is another story.

I can write a lot about it, but I do not know, if kallzz is using C++.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:monkesdb
ID: 9635850
if you want to start using managed C++ you can use the __sealed keyword to stop anything deriving from it. but for this you will need .NET

__sealed class MyClass
{...}
0
 

Author Comment

by:kallzz
ID: 9639697
Iam Using c++
0
 
LVL 1

Accepted Solution

by:
fsign21 earned 50 total points
ID: 9641115
Here is a cut & paste from
Bjarne Stroustrup's C++ Style and Technique FAQ
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

Can I stop people deriving from my class?

Yes, but why do you want to? There are two common answers:

    * for efficiency: to avoid my function calls being virtual
    * for safety: to ensure that my class is not used as a base class (for example, to be sure that I can copy objects without fear of slicing)

In my experience, the efficiency reason is usually misplaced fear. In C++, virtual function calls are so fast that their real-world use for a class designed with virtual functions does not to produce measurable run-time overheads compared to alternative solutions using ordinary function calls. Note that the virtual function call mechanism is typically used only when calling through a pointer or a reference. When calling a function directly for a named object, the virtual function class overhead is easily optimized away.

If there is a genuine need for "capping" a class hierarchy to avoid virtual function calls, one might ask why those functions are virtual in the first place. I have seen examples where performance-critical functions had been made virtual for no good reason, just because "that's the way we usually do it".

The other variant of this problem, how to prevent derivation for logical reasons, has a solution. Unfortunately, that solution is not pretty. It relies on the fact that the most derived class in a hierarchy must construct a virtual base. For example:

      class Usable;

      class Usable_lock {
            friend class Usable;
      private:
            Usable_lock() {}
            Usable_lock(const Usable_lock&) {}
      };

      class Usable : public virtual Usable_lock {
            // ...
      public:
            Usable();
            Usable(char*);
            // ...
      };

      Usable a;

      class DD : public Usable { };

      DD dd;  // error: DD::DD() cannot access
              // Usable_lock::Usable_lock(): private  member
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 211
count7 challenge 12 103
mapAB Challlenge 35 126
control image tags in a string ? 12 111
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now