dchhetri
dchhetri

Reputation: 7136

Forward declare a inner class type, is it possible?

Ok here is the situation.

//foo.h
struct A1{ 
 struct A2{};
};

//bar.h
#include "MyString.h"
class A2; //note, not including foo.h
TEMPLATE_INSTIANTIATE_MAP_OF_TYPE(String,A2*); //assume compiler doesn't do this

Is it possible to make the above situation work? I try to create a MyMap<String,A1::A2*> m; but the compilers throws undefined reference errors. Is it possible to make the above work without having bar.h import foo.h?

Upvotes: 2

Views: 1202

Answers (2)

Ram
Ram

Reputation: 3133

Here is a way to declare nested classes outside a class definition. class Logic is the outer class. LogicImp is the forward declared struct.

class Logic
{
public:

    Logic();
    ~Logic();

private:
    struct LogicImp;
    std::unique_ptr<LogicImp> limp_;
};

struct Logic::LogicImp
{
    int nLogical_;
};

Logic::Logic():limp_(new LogicImp())
{
}

Logic::~Logic()
{
}

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254471

Sadly, it isn't. Nested classes can only be declared inside a class definition.

Upvotes: 2

Related Questions