Eliezer
Eliezer

Reputation: 7347

C++ Have One Nested Class Inherit From Another Nested Class

I'm writing a cross-platform class hierarchy, and want to keep the platform dependent implementations in their own class (as opposed to having one class with #ifdefs). This is what I have so far, but the compiler is complaining that BaseDef is private. Any help with how I could keep this basic structure while getting it to compile would be greatly appreciated :-)

Edit: It would seem from here that this isn't possible. Any other way I could keep this general structure and still compile?

Root.h

class Root {
    private:
        class BaseDef {
            virtual void foo() = 0;
            virtual void bar() = 0;
        };

        #ifdef _WIN32
        class WinImp;
        #else
        class NixImp;
        #endif

        BaseDef* imp;

        BaseDef* getImp();

    public:
        Root() : imp(getImp()) {}
        void foo();
        void bar();
};

Root.cpp

#include "Root.h"
void Root::foo() {
    imp->foo();
}

void Root::bar() {
    imp->bar();
}

WinImp.h

#ifdef _WIN32
#include "Root.h"
class WinImp : public Root::BaseDef {
    public:
        void foo();
        void bar();
};
#endif

WinImp.cpp

#include "WinImp.h"
#ifdef _WIN32
    Root::WinImp::foo() {

    }

    Root::WinImp::bar() {

    }

    Root::BaseDef* Root::getImp() {
        return new Root::WinImp();
    }
#endif

Upvotes: 0

Views: 842

Answers (2)

John5342
John5342

Reputation: 1149

Your main problem is that BaseDef is private. That means that other classes (aside from Root itself) cannot access the BaseDef name. One way is to make BaseDef public. Alternatively you can make the derived classes (WinImp and NixImp) friends of Root so that they can access the BaseDef name. In addition Root cannot access the members of BaseDef so they need to be public or make Root a friend of BaseDef.

class Root {
private:
  class BaseDef {
  public:
    // These need to be public so that Root can see them or Root needs to be a friend. 
    //Nothing else can see BaseDef though so this is safe.
    virtual void foo() = 0;
    virtual void bar() = 0;
  };

  class WinImp; // Forward declare the classes
  friend class WinImp; // And then make them friends
  class NixImp;
  friend class NixImp;

  BaseDef* imp;

  BaseDef* getImp();

public:
  Root() : imp(getImp()) {}
  void foo();
  void bar();
};

void Root::foo() {
  imp->foo();
}

void Root::bar() {
  imp->bar();
}

// Since this is a nested class i made it Root::WinImp
class Root::WinImp : public Root::BaseDef {
public:
  void foo();
  void bar();
};

void Root::WinImp::foo() {}

void Root::WinImp::bar() {}

Root::BaseDef* Root::getImp() {
  return new WinImp();
}

This method is not allowed according to the 2003 standard (11.4.p2) but in C++11 (same example) it is explicitly allowed (11.3.p2). However, clang (3.1 tested) accepts this even in 2003 mode. gcc (4.7.2 tested) accepts this (even in 2003 mode) so long as the derived classes are nested inside the same class but not if outside the class.

Upvotes: 1

Dietrich Epp
Dietrich Epp

Reputation: 213318

It's complaining that BaseDef is private...

class Root {
    private:
        class BaseDef {
            virtual void foo() = 0;
            virtual void bar() = 0;
        };

So make it public...

class Root {
    public:
        class BaseDef {
            virtual void foo() = 0;
            virtual void bar() = 0;
        };

Footnote:

You're trying to avoid #ifdef, so get rid of this:

#ifdef _WIN32
class WinImp;
#else
class NixImp;
#endif

Instead, just use a single class:

class Imp;

Upvotes: 1

Related Questions