Click here to Skip to main content
15,891,607 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: can we make class cant be derived at all? Pin
theCPkid23-Oct-09 22:00
theCPkid23-Oct-09 22:00 
GeneralRe: can we make class cant be derived at all? Pin
Rajesh R Subramanian23-Oct-09 22:14
professionalRajesh R Subramanian23-Oct-09 22:14 
GeneralRe: can we make class cant be derived at all? Pin
theCPkid23-Oct-09 22:25
theCPkid23-Oct-09 22:25 
GeneralRe: can we make class cant be derived at all? Pin
Rajesh R Subramanian23-Oct-09 22:36
professionalRajesh R Subramanian23-Oct-09 22:36 
GeneralRe: can we make class cant be derived at all? Pin
Richard MacCutchan24-Oct-09 7:15
mveRichard MacCutchan24-Oct-09 7:15 
GeneralRe: can we make class cant be derived at all? Pin
Rajesh R Subramanian24-Oct-09 7:38
professionalRajesh R Subramanian24-Oct-09 7:38 
AnswerRe: can we make class cant be derived at all? Pin
Rajesh R Subramanian23-Oct-09 19:31
professionalRajesh R Subramanian23-Oct-09 19:31 
AnswerRe: can we make class cant be derived at all? Pin
Kushagra Tiwari23-Oct-09 21:33
Kushagra Tiwari23-Oct-09 21:33 
Hi ,

Here is what Stroutstroup had suggested on the same thing you want to achieve:

Can I stop people deriving from my class?

Bjarne Stroustrup :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

I thinks This solves your problem , If yes rate the answer as Good and close the thread.

Regards,
Kushagra

I hate to code but I luv to Develop Smile | :)
AnswerRe: can we make class cant be derived at all? [modified] Pin
Nuri Ismail23-Oct-09 22:27
Nuri Ismail23-Oct-09 22:27 
Questionscanner Driver Pin
randydom23-Oct-09 11:03
randydom23-Oct-09 11:03 
QuestionRe: scanner Driver Pin
NitinMakwana8-Jan-10 1:10
NitinMakwana8-Jan-10 1:10 
QuestionMemory Leak Pin
BarryPearlman23-Oct-09 10:51
BarryPearlman23-Oct-09 10:51 
AnswerRe: Memory Leak Pin
«_Superman_»23-Oct-09 12:21
professional«_Superman_»23-Oct-09 12:21 
AnswerRe: Memory Leak Pin
BarryPearlman25-Oct-09 3:56
BarryPearlman25-Oct-09 3:56 
GeneralRe: Memory Leak Pin
«_Superman_»25-Oct-09 8:43
professional«_Superman_»25-Oct-09 8:43 
AnswerRe: Memory Leak Pin
PJ Arends23-Oct-09 12:37
professionalPJ Arends23-Oct-09 12:37 
GeneralRe: Memory Leak Pin
BarryPearlman23-Oct-09 15:12
BarryPearlman23-Oct-09 15:12 
Questionint treated as binary no. Pin
Manmohan2923-Oct-09 10:41
Manmohan2923-Oct-09 10:41 
AnswerRe: int treated as binary no. Pin
David Crow23-Oct-09 10:45
David Crow23-Oct-09 10:45 
GeneralRe: int treated as binary no. Pin
Manmohan2923-Oct-09 10:58
Manmohan2923-Oct-09 10:58 
AnswerRe: int treated as binary no. Pin
David Crow23-Oct-09 11:04
David Crow23-Oct-09 11:04 
GeneralRe: int treated as binary no. Pin
Manmohan2923-Oct-09 11:16
Manmohan2923-Oct-09 11:16 
GeneralRe: int treated as binary no. Pin
David Crow23-Oct-09 15:08
David Crow23-Oct-09 15:08 
QuestionScrollbars are shown incorrectly. Pin
Nikz223-Oct-09 10:01
Nikz223-Oct-09 10:01 
AnswerRe: Scrollbars are shown incorrectly. [modified] Pin
Nikz223-Oct-09 12:17
Nikz223-Oct-09 12:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.