238

I recently got stuck in a situation like this:

class A
{
public:
    typedef struct/class {…} B;
…
    C::D *someField;
}

class C
{
public:
    typedef struct/class {…} D;
…
    A::B *someField;
}

Usually you can declare a class name:

class A;

But you can't forward declare a nested type, the following causes compilation error.

class C::D;

Any ideas?

3

7 Answers 7

267

You can't do it, it's a hole in the C++ language. You'll have to un-nest at least one of the nested classes.

6
  • 7
    Thanks for the answer. In my case, they're not my nested classes. I was hoping to avoid a huge library header file dependency with a little forward reference. I wonder if C++11 fixed it?
    – Marsh Ray
    Nov 7, 2011 at 0:57
  • 75
    Oh. Just what I didn't want google to show up. Thanks anyway for the concise answer.
    – learnvst
    Jan 11, 2012 at 22:44
  • 22
    Same here ... does someone know why it is not possible ? It seems there is valid use cases, and this lack prevents architecture consistency in some situations. Nov 1, 2012 at 16:10
  • You can use friend. And just put a comment in that you're using it to work around the hole in C++. Jun 14, 2016 at 16:31
  • 5
    Whenever I encounter such needless flaws in this ersatz language, I am torn between laughing and crying Aug 16, 2016 at 1:30
40
class IDontControl
{
    class Nested
    {
        Nested(int i);
    };
};

I needed a forward reference like:

class IDontControl::Nested; // But this doesn't work.

My workaround was:

class IDontControl_Nested; // Forward reference to distinct name.

Later when I could use the full definition:

#include <idontcontrol.h>

// I defined the forward ref like this:
class IDontControl_Nested : public IDontControl::Nested
{
    // Needed to make a forwarding constructor here
    IDontControl_Nested(int i) : Nested(i) { }
};

This technique would probably be more trouble than it's worth if there were complicated constructors or other special member functions that weren't inherited smoothly. I could imagine certain template magic reacting badly.

But in my very simple case, it seems to work.

3
  • 21
    In C++11 you can inherit constructors by using basename::basename; in the derived class, thus no problem with complicated ctors.
    – Xeo
    Nov 7, 2011 at 1:27
  • 1
    Nice trick, but it will not work if pointer to IDontControl::Nested used within same header (where it forward declared) and accessed from external code which also includes full definition of IDontControl. (Because compiler will not match IDontControl_Nested and IDontControl::Nested). Workaround is to perform static cast. Nov 18, 2015 at 4:56
  • 1
    I'd recommend doing the opposite and having the class outside, and just use typedef inside the class
    – ridderhoff
    Mar 1, 2019 at 8:21
5

If you really want to avoid #including the nasty header file in your header file, you could do this:

hpp file:

class MyClass
{
public:
    template<typename ThrowAway>
    void doesStuff();
};

cpp file

#include "MyClass.hpp"
#include "Annoying-3rd-party.hpp"

template<> void MyClass::doesStuff<This::Is::An::Embedded::Type>()
{
    // ...
}

But then:

  1. you will have to specify the embedded type at call time (especially if your function does not take any parameters of the embedded type)
  2. your function can not be virtual (because it is a template)

So, yeah, tradeoffs...

2
  • 1
    What the heck is an hpp file?
    – Naftali
    Apr 16, 2012 at 12:58
  • 12
    lol, an .hpp header file is used in C++ projects to distinguish it from a C header file which typically ends with .h. When working with C++ and C in the same project some people prefer .hpp and .cpp for C++ files, to make it explicitly with what type of files their dealing with, and .h and .c for C files.
    – Alex Bitek
    Dec 3, 2012 at 14:16
2

If you have access to change the source code of classes C and D, then you can take out class D separately, and enter a synonym for it in class C:

class CD {

};

class C {
public:

    using D = CD;

};

class CD;
1

I would not call this an answer, but nonetheless an interesting find: If you repeat the declaration of your struct in a namespace called C, everything is fine (in gcc at least). When the class definition of C is found, it seems to silently overwrite the namspace C.

namespace C {
    typedef struct {} D;
}

class A
{
public:
 typedef struct/class {...} B;
...
C::D *someField;
}

class C
{
public:
   typedef struct/class {...} D;
...
   A::B *someField;
}
3
  • 1
    I tried this with cygwin gcc and it doesn't compile if you try to reference A.someField. C::D in the class A definition actually refers the the (empty) struct in the namespace, not the struct in the class C (BTW this doesn't compile in MSVC)
    – Dolphin
    Jun 4, 2009 at 16:44
  • It gives the error: "'class C' redeclared as different kind of symbol"
    – Calmarius
    Jun 4, 2009 at 16:53
  • 9
    Looks like a GCC bug. It seems to think a namespace name can hide a class name in the same scope. Jun 4, 2009 at 17:32
0

This would be a workaround (at least for the problem described in the question -- not for the actual problem, i.e., when not having control over the definition of C):

class C_base {
public:
    class D { }; // definition of C::D
    // can also just be forward declared, if it needs members of A or A::B
};
class A {
public:
    class B { };
    C_base::D *someField; // need to call it C_base::D here
};
class C : public C_base { // inherits C_base::D
public:
    // Danger: Do not redeclare class D here!!
    // Depending on your compiler flags, you may not even get a warning
    // class D { };
    A::B *someField;
};

int main() {
    A a;
    C::D * test = a.someField; // here it can be called C::D
}
0

This can be done by forward declare the outer class as a namespace.

Sample: We have to use a nested class others::A::Nested in others_a.h, which is out of our control.

others_a.h

namespace others {
struct A {
    struct Nested {
        Nested(int i) :i(i) {}
        int i{};
        void print() const { std::cout << i << std::endl; }
    };
};
}

my_class.h

#ifndef MY_CLASS_CPP
// A is actually a class
namespace others { namespace A { class Nested; } }
#endif

class MyClass {
public:
    MyClass(int i);
    ~MyClass();
    void print() const;
private:
    std::unique_ptr<others::A::Nested> _aNested;
};

my_class.cpp

#include "others_a.h"
#define MY_CLASS_CPP // Must before include my_class.h
#include "my_class.h"

MyClass::MyClass(int i) :
    _aNested(std::make_unique<others::A::Nested>(i)) {}
MyClass::~MyClass() {}
void MyClass::print() const {
    _aNested->print();
}
2
  • 1
    It may work, but it is undocumented. The reason why it work is that a::b is mangled the same way, no matter if a is class or namespace. Feb 26, 2019 at 15:40
  • 8
    Doesn't work with Clang or GCC. It says that the outer class was declared as something different than a namespace.
    – Dugi
    May 1, 2019 at 19:47

Your Answer

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

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