使用 final 关键字修饰的类不能被继承。
C++
#include <iostream>
using namespace std;
class Base final
{
};
class Derive: public Base{ // error: cannot derive from 'final' base 'Base' in derived type 'Derive'
};
int main()
{
Derive ex;
return 0;
}
C++
#include <iostream>
using namespace std;
template <typename T>
class Base{
friend T;
private:
Base(){
cout << "base" << endl;
}
~Base(){}
};
class B:virtual public Base<B>{ //一定注意 必须是虚继承
public:
B(){
cout << "B" << endl;
}
};
class C:public B{
public:
C(){} // error: 'Base<T>::Base() [with T = B]' is private within this context
};
int main(){
B b;
return 0;
}
说明:在上述代码中 B 类是不能被继承的类。
具体原因:
阅读量:2015
点赞量:0
收藏量:0