🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2331

[Q]: Forward declaring nested classes/types

#2331
From: Sheetal V. Kakkad
Date: Fri, 27 Oct 1995 03:06
51 lines
2752 bytes
Is it possible to forward declare nested classes in C++? For example,
if I have a class "Inner" defined inside a class "Outer" (in some 
header file), I would like to have a forward declaration (in another 
file) such as:

class Outer::Inner;

One of the reasons for needing such a forward declaration is for use
in a function prototype declaration as follows:

class Outer::Inner;
void func (Outer::Inner *p);

The definitions of classes "Outer" and "Inner" are in some other
header file that I should not have to include in this file in order
for the prototype of "func" to be valid. It could be argued that class
Inner should never be visible outside class Outer, but I am not sure if
that would be valid. Examples are typedef for iterators in STL container
classes, which can be accessed from global scope with appropriate
scoping (for example, "vector<int>::iterator"). I also don't know if it
matters whether the Inner class is defined in public or private scope.

The compilers I tried (g++ 2.7.0 and Sun C++ 3.0.1) do not like the 
above forward declaration. What is the correct syntax of achieving this
(if any)? I know that I can provide a forward declaration just for class
Inner as follows:

class Outer
{
  class Inner;
};

But this has the side-effect of *defining* class Outer, which is
obviously undesirable (since the real definition of class Outer is
in some other file).

Is there a general way of forward declaring both the outer and inner
classes? What are/will be the ramifications of that for templates
(both for inner and outer classes)? Alternatively, if forward 
declarations are prohibited in the language, what is the reasoning 
behind such a decision?

Thanks in advance,
-- 
----------------------------------------------------------------------
Sheetal V. Kakkad           Department of Computer Sciences, UT-Austin
svkakkad@cs.utexas.edu        http://www.cs.utexas.edu/users/svkakkad/
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <46p8ks$fmv@boogie.cs.utexas.edu>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 27 Oct 95 03:06:46 GMT
Approved: fjh@cs.mu.oz.au
From: svkakkad@cs.utexas.edu (Sheetal V. Kakkad)
Newsgroups: comp.std.c++
Subject: [Q]: Forward declaring nested classes/types
X-Original-Date: 26 Oct 1995 19:20:44 -0500
Organization: CS Dept, University of Texas at Austin
Message-ID: <46p8ks$fmv@boogie.cs.utexas.edu>
Keywords: nested classes, forward declaration
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMJBM3OEDnX0m9pzZAQHj9AF/c6J4v6Fjd7B8oTfJh8CNgUV6MgT5WwXf
 r7QNCd5fmFEsG76bXA3FoiIZBfANrlqa
 =OaG9