Virtual function access level in derived class

C

cppquester

I figured that a private virtual function is called through the base
class when the function is declared there public.
I think this is fine, but it was compiled without warning.
I use gcc, is this behaviour guaranteed in the standard?

Thanks,
Marc
 
K

Kai-Uwe Bux

cppquester said:
I figured that a private virtual function is called through the base
class when the function is declared there public.
I think this is fine, but it was compiled without warning.
I use gcc, is this behaviour guaranteed in the standard?

Yes, see [11.6/1]:

The access rules (clause 11) for a virtual function are determined by its
declaration and are not affected by the rules for a function that later
overrides it. [Example:

class B {
public:
virtual int f();
};

class D : public B {
private:
int f();
};

void f()
{
D d;
B* pb = &d;
D* pd = &d;
pb->f(); //OK: B::f() is public,
// D::f() is invoked
pd->f(); //error: D::f() is private
}
?end example] ...


Best

Kai-Uwe Bux
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top