Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ nested class/forward declaration issue

Is it possible to forward-declare a nested class, then use it as the type for a concrete (not pointer to/reference to) data member of the outer class?

I.E.

class Outer;  class Outer::MaybeThisWay   // Error: Outer is undefined { };  class Outer {  MaybeThisWay x;   class MaybeThatOtherWay;   MaybeThatOtherWay y;   // Error: MaybeThatOtherWay is undefined }; 
like image 936
uj2 Avatar asked Apr 08 '10 13:04

uj2


People also ask

Can I forward declare a nested class?

You cannot forward declare a nested structure outside the container. You can only forward declare it within the container.

Does C support forward declaration?

Classes. In some object-oriented languages like C++ and Objective-C, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.

How do you declare a nested class?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

What is the purpose of forward class declaration?

A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.


2 Answers

You can't forward-declare a nested class like that.

Depending on what you're trying to do, maybe you can use a namespace rather than a class on the outer layer. You can forward-declare such a class no problem:

namespace Outer {    struct Inner;  };  Outer::Inner* sweets;  // Outer::Inner is incomplete so                         // I can only make a pointer to it 

If your Outer absolutely must be a class, and you can't shoe-horn it into a namespace, then you'll need for Outer to be a complete type in the context where you forward declare Inner.

class Outer {    class Inner;  // Inner forward-declared };  // Outer is fully-defined now  Outer yes;  // Outer is complete, you can make instances of it Outer::Inner* fun;  // Inner is incomplete, you can only make                      // pointers/references to it  class Outer::Inner  { };  // now Inner is fully-defined too  Outer::Inner win;  // Now I can make instances of Inner too 
like image 146
janks Avatar answered Sep 24 '22 12:09

janks


There is no way to forward declare a nested class without fully specifying the containing class. This little trick kinda fixes the problem though

class Outer_Inner { };  class Outer { public:    typedef Outer_Inner Inner; }; 

This works for me as in my naming convention Outer_Inner isn't a valid class name, so it's obvious that it refers to an nested class.

You still can't forward declare the nested class like this:

class Outer::Inner; 

But at least it can be forward declared with:

class Outer_Inner; 

If you don't like the way Outer_Inner looks you could adopt a naming convention for nested classes that better suits your tastes. Outer__Inner, Outer_nested_Inner, etc.

like image 24
deft_code Avatar answered Sep 25 '22 12:09

deft_code