26

I'm having a problem similar to that described in Private inheritance renders class inaccessible where a privately inherited base class gives an "inaccessible within this context" error when I try to declare a member of the base class inside the derived class.

Explicitly referencing X with ::X works in the above case, but what if the code is in a function such as:

void fooby()
{
    class X {};

    class Y : private X {};

    class Z : public Y
    {
    public:
        X x; // Compiler "inaccessible within this context" error
    };
};

How do you reference X in this case?

If fooby were a struct/class, then ::fooby::X would work, but I'm not sure how to do it in the case above.

2
  • What are you trying to do having both a member and base class of the same type? Maybe that would help with an alternate suggestion.
    – Mark B
    Aug 26, 2011 at 17:31
  • @Mark B - this is just a simple contrived example to illustrate the behavior that I'm trying to understand Sep 4, 2011 at 13:18

2 Answers 2

15

The problem that you are facing is that there is an injected identifier X in Y (and all derived types) that refers to X, which is not accessible below Y.

In the common case of user-defined types that are declared at namespace level, you could use the namespace to qualify the type and gain access:

class X {};
class Y : X {};
class Z : Y {
   ::X x;            // Or Namespace::X
};

Because you are defining your types inside a function that is not a valid option.

Alternatively, you can get around the problem with other workarounds. As hamstergene proposed, you can create an alternative identifier to refer to X:

typedef class X {} another_name;
class Y : X {};
class Z : Y {
   another_name x;
};

Or you can add a typedef inside Y to provide the type to derived types:

class X {};
class Y : X {
public:
   typedef X X;
};
class Z : Y {
   X x;
};

This last option works, because it will add an X identifier inside Y that is public and refers to the type, so the compiler will find the type there and use that in Z.

3
  • +1 great answer and I learned about using class in a different way today.
    – Mark B
    Aug 26, 2011 at 18:28
  • Your "next option" ("class X x;") is wrong. That won't work - it's just the same and refers to the injected class name privately inherited and thus inaccessible. You seem to be in the misconception that the injected class name is an object rather than a type. The injected class name refers to a class and is a type name. Aug 26, 2011 at 21:13
  • @Johannes: Yes, you are right in that I had that misconception. I was actually trying to look this up in the standard (g++ accepts it 4.2/4.7, but clang++ and comeau reject it). It seems that both the class X x; and the identity<X>::type fail. Aug 26, 2011 at 21:16
9

I can't find a way to do that. The only option I see is to introduce typedef outside of Z:

typedef X PITA;

class Z : public Y
{
public:
    PITA x; // ok
};
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.