hack to make a member function final

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajib

    hack to make a member function final

    Not that this serves any real purpose, but gcc allows me to do some hack
    like this:

    class hide_A {
    public:
    class A {
    public:
    virtual int final() { return 42; }
    };

    class A_finalize {
    public:
    virtual void final() {}
    };
    friend class B;
    };

    class B : public hide_A::A, private hide_A::A_final ize {
    public:
    void final() { } //error
    };


    You can't use A directly and have to access it through B. But you can't
    override final() because it will have a conflicting return type no
    matter what you do.

    Is this valid standard C++? Anyone see any way to circumvent this or any
    major problems (other than having no practical value)?

    -Rajib
  • Mr. B

    #2
    Re: hack to make a member function final

    Rajib wrote:
    Not that this serves any real purpose, but gcc allows me to do some hack
    like this:
    >
    class hide_A {
    public:
    class A {
    public:
    virtual int final() { return 42; }
    };
    >
    class A_finalize {
    public:
    virtual void final() {}
    };
    friend class B;
    };
    >
    class B : public hide_A::A, private hide_A::A_final ize {
    public:
    void final() { } //error
    };
    >
    >
    You can't use A directly and have to access it through B. But you can't
    override final() because it will have a conflicting return type no
    matter what you do.
    This is not true, class A is a public class in hide_A. Also, changing the
    access to private results in an error.
    Is this valid standard C++? Anyone see any way to circumvent this or any
    major problems (other than having no practical value)?
    You can get around the restriction by introducing another class, C, and
    having B only inherit from the A_finalize class:

    class B : private hide_A::A_final ize
    {
    public:
    void final(){}
    };

    class C : public B, public hide_A::A
    {};

    And now you just have client code use class C. Not sure if this is what you
    were looking for, though.

    -- B

    Comment

    Working...