David Doria
David Doria

Reputation: 10273

How to skip a class with public access to a function?

I have this setup:

class A
{
  public:
  virtual void Function();
}

class B : private A
{

}

class C : public B
{
public:
// I want to expose A::Function() here
}

I tried to do this by adding:

class C : public B
{
public:
  virtual void Function();
}

and

C::Function()
{
  A::Function();
}

but I get and "inaccessible base" error.

Is it possible to do something like this?

Upvotes: 1

Views: 132

Answers (4)

James McNellis
James McNellis

Reputation: 355187

In B you can change the accessibility of A::Function to protected:

class B : private A
{
protected:
    using A::Function;
};

In C::Function (and elsewhere in C) you will then have to refer to the function as B::Function, not A::Function. You could also public: using B::Function; in C instead of implementing a C::Function that just calls B::Function.

Upvotes: 5

John Percival Hackworth
John Percival Hackworth

Reputation: 11531

Not really. The scenario you're describing would indicate that your class hierarchy needs to be reworked.

Upvotes: 0

Puppy
Puppy

Reputation: 146968

You can't do this. The fact that B inherits from A is an implementation detail and you are not allowed to access it from C- just like you can't access B's private functions or member variables.

This would be completely legal if B inherited protected or public from A.

Upvotes: 3

hammar
hammar

Reputation: 139890

If you can change it to class B : protected A it should work.

Upvotes: 1

Related Questions