🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Sheetal V. Kakkad Fri, 27 Oct 1995 03:06
[Q]: Forward declaring nested classes/types
#2331
Author: 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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads