Forward declaration of derived inner class

Posted by Loom on Stack Overflow See other posts from Stack Overflow or by Loom
Published on 2013-10-29T15:19:06Z Indexed on 2013/10/29 15:54 UTC
Read the original article Hit count: 271

I ran into problem implementing some variations of factory method.

// from IFoo.h

struct IFoo {

  struct IBar {
    virtual ~IBar() = 0;
    virtual void someMethod() = 0;
  };

  virtual IBar *createBar() = 0;
};

// from Foo.h 
struct Foo : IFoo { // implementation of Foo, Bar in Foo.cpp

  struct Bar : IBar { 
    virtual ~Bar();
    virtual void someMethod();
  };

  virtual Bar *createBar(); // implemented in Foo.cpp
};    

I'd like to place declaration of Foo::Bar in Foo.cpp. For now I cannot succeed:

struct Foo : IFoo {

  //struct Bar;        //1. error: invalid covariant return type 
                       //   for ‘virtual Foo::Bar* 
  //struct Bar : IBar; //2. error: expected ‘{’ before ‘;’ token

  virtual Bar *createBar(); 
  // virtual IBar *createBar(); // Is not acceptable by-design
};    

Is there a trick to have just forward declaration of Boo in Foo.hpp and to have full declaration in Foo.cpp?

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance