How would you stop a class from class from being derived or
inherited.
Answer Posted / srini
#define DO_NOT_DERIVE(T) \
class NoDerive_##T { \
friend class T;
\
NoDerive_##T() {}
\
NoDerive_##T(const NoDerive_##T&) {} \
};
#define stopderiving(T) private virtual NoDerive_##T
when we derive from any class, the constructor needs to be
called, since here constructor of NoDerive calss is
private, it cannot be derived. And friendship cannot be
inherited.
usage
class finalclass : stopderiving (finalclass)
{
....
};
the other possible solution for this is making the default
constructor private.
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
What causes polymorphism?
Can private class be inherited?
What is difference between polymorphism and inheritance?
Can we create object of abstract class?
What is the renewal class?
What is polymorphism and why is it important?
What is destructor give example?
Can we create object of interface?
What is class in oop with example?
What is inheritance and how many types of inheritance?
What is the difference between inheritance and polymorphism?
Why do we use inheritance?
Is oop better than procedural?
#include
Why do we use oops?