final class in c++

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

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


        
7条回答
  •  -上瘾入骨i
    2020-12-31 09:14

    Curiously recurring template pattern. Use private inheritence.

    template< typename T > class Final
    {
    protected:
        Final() {}
        Final( Final const& ) {}
    };
    
    class X : private virtual Final
    {
      // whatever I want to do
    };
    

    and you should find it impossible to derive anything from X because the virtual inheritence means that the most-derived class must construct the base class but it won't have any access to it.

    (I haven't tested this code).

提交回复
热议问题