hack to make a member function final

R

Rajib

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
 
M

Mr. B

Rajib said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top