final class in c++

前端 未结 7 1632
轮回少年
轮回少年 2020-12-31 08:36
class Temp
{
private:
    ~Temp() {}
    friend class Final;
};

class Final : virtual public Temp
{
public:
     void fun()
     {
         cout<<\"In base\";         


        
7条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 09:00

    Note that non-inheritable classes exist in C++11 using the final keyword, specified before the : base1, base2, ..., baseN inheritance list or before the opening { if the class inherits from nothing:

    class Final final { };
    class Derived : public Final { }; // ERROR
    

    With a little macro magic and some compiler-detection effort this can be abstracted away to work, or at worst do nothing, on all compilers.

提交回复
热议问题