Philippe
Philippe

Reputation: 1307

error on private inheritance and function overloading

I have a problem which I narrowed down to the following code:

class A
{
};

class B : private A
{
};

void f(A*)
{
}

void f(void*)
{
}

int main()
{
  B b;
  f(&b);
}

Which gives the following error with gcc 4.7:

error: ‘A’ is an inaccessible base of ‘B’

I know that A is inaccessible but I would have liked the compiler to call f(void*). Is this behavior normal or am I doing something wrong? Or maybe it's a compiler bug?

Upvotes: 3

Views: 119

Answers (2)

CornSmith
CornSmith

Reputation: 2037

You need to make sure that b is being passed in as a void *, which won't be the default case since b is actually an instance of A. Just explicitly tell f() that b is void * by casting it:

class A {
};

class B : private A {
};

void f(A*) {
}

void f(void*) {
}

int main() {
  B b;
  f((void*)&b);
}

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76245

Overloading is resolved before access checking. So the compiler chooses f(A*) as the appropriate overload, then determines that &b can't be converted to A* and gives the error message.

Upvotes: 4

Related Questions