How to make a class cannot be inherited in C ++

1 Use final

EG:

class B final
{

};

 

2 Use friend class and virtual inheritance

EG:

class A
{
private:
	A()
	{
		cout << "A" << endl;
	}
	friend class B;
};

class B:virtual public A
{

};

In the above example, class B cannot be inherited, because if a class inherits B, because B virtual inherits A, the class that inherits B must call the constructor of A when constructing, but because the constructor of class A Private type, so contradictory

Published 123 original articles · praised 31 · 90,000 views +

Guess you like

Origin blog.csdn.net/qq_40794602/article/details/103914861