Passing a pointer to a type that is privately inherited in some base class

This happens due to injected-class-name, unqualified name lookup rules and the fact the name lookup is completed before check for accessibility.

injected-class-name is a mechanism that makes class name available inside that class definition.

Now, the unqualified name lookup rules within a class definition state that first the scope of the class is searched, then the scopes of any base classes are searched recursively, and only after that (and some more steps) you perform normal search in namespace scope.

Putting this all together:

  1. There are 2 names MyInterface in scope of Inherited – one as injected-class-name and one that resides in the same namespace as Inherited (the global namespace).
  2. Name lookup in Inherited first finds MyInterface as injected-class-name inherited from MyImpl. Name lookup is satisfied and doesn’t search any longer for other instances of the name.
  3. However, the name MyInterface inherited from MyImpl is not accessible to Inherited, because there is private inheritance – an error happens.

The way to fix that is to change unqualified name lookup into qualified one:

struct Inherited : public MyImpl {
    void doSomething(::MyInterface* mi) {}
};

Now, injected-class-name cannot satisfy name lookup, because you explicitly ask for MyInterface from global namespace, not any MyInterface that happens to match. And since the name MyInterface in global namespace is public (like all namespace names), it can used without any issue.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)