hack to make a member function final

Discussion in 'C++' started by Rajib, Aug 3, 2008.

  1. Rajib

    Rajib Guest

    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_finalize {
    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
    Rajib, Aug 3, 2008
    #1
    1. Advertising

  2. Rajib

    Mr. B Guest

    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_finalize {
    > 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_finalize
    {
    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
    Mr. B, Aug 3, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,732
  2. Newsgroup - Ann
    Replies:
    5
    Views:
    600
    John Carson
    Jul 30, 2003
  3. slide_o_mix
    Replies:
    0
    Views:
    420
    slide_o_mix
    Oct 15, 2003
  4. Replies:
    5
    Views:
    510
    Chris Uppal
    Nov 17, 2006
  5. szczepiq
    Replies:
    4
    Views:
    349
Loading...

Share This Page