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


Please Help Members By Posting Answers For Below Questions

What is the difference between a mixin and inheritance?

525


What is static in oop?

591


What does enum stand for?

617


What is polymorphism used for?

577


What is the oops and benefits of oops programming?

558






What is the difference between inheritance and polymorphism?

594


What is new keyword in oops?

593


What is constructor in oop?

591


What exactly is polymorphism?

612


What is an advantage of polymorphism?

599


Whats oop mean?

601


Why is there no multiple inheritance?

572


What is the difference between abstraction and polymorphism?

619


How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction

1657


What is difference between oop and pop?

620