C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Forward declaration of nested classes.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alan McKenney
Guest





PostPosted: Mon Sep 29, 2003 6:24 am    Post subject: Forward declaration of nested classes. Reply with quote



I cannot figure out how to forward declare a class
that
is in a namespace or nested in another class.


I like to define classes that relate to the definition
of a class within the class; for example, if I define
a table class StockTable, I like to define the
class that represents one row (or "entry") as
StockTable::Entry. E.g.,

class StockTable {
public:
class Entry { ... };
class Key { ... };
std::vector<Key, Entry> table;
....
};

I also like to define classes within namespaces, e.g.,

namespace A {
class C { .... };

};

I also like to use forward declarations, where
possible, to avoid having header files bring in more
definition than necessary. Thus, if, say, class X
only uses pointers or references to class Y, I will
write something like


class Y;
class X {
void f( Y *y );
const Y &g();
....
};However, if the class Y is nested, or defined
in a
namespace, this doesn't work. The obvious way to
do it:

class StockTable::Entry;
class A::C;
class Y {
void f( StockTable::Entry *e );
A::C *curr_ccc;
...
};

gives a compile error. Forward declaring "StockTable"

or "A" doesn't help. The only thing I have found that
works is including the full definition of StockTable,
which includes "Entry", and "A", which includes "C".


Primary Question:

Is it possible to forward declare Entry or C,
without including the definition of StockTable
or A?
Discussion Question:

Is there a good reason not to allow forward
declarations of the form "class A::C;"?

To me, it seems as ambiguous or unambiguous as
"class B;": it says that A::C is a class, which
will be defined within a scope "A".

Until defined, you can only declare pointers and
refs to it.

In order to do anything else, you need to define
"A" and "A::C" completely.

Or am I missing something?


Alan McKenney
[email]alan_mckenney1 (AT) yahoo (DOT) com[/email]

If so, how?







__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jonathan Mcdougall
Guest





PostPosted: Mon Sep 29, 2003 10:10 pm    Post subject: Re: Forward declaration of nested classes. Reply with quote



Quote:
I cannot figure out how to forward declare a class
that
is in a namespace

// definition
namespace N
{
class A
{
};
}

// declaration
namespace N
{
class A;
}


Quote:
or nested in another class.

No, you must have the full definition of the outer class :

class outer
{
class inner;
};

That is a declaration of the 'inner' class.

Quote:
Discussion Question:

please take them to comp.std.c++


Jonathan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Tue Sep 30, 2003 10:36 am    Post subject: Re: Forward declaration of nested classes. Reply with quote



Alan McKenney <alan_mckenney1 (AT) yahoo (DOT) com> wrote in message

Quote:
I cannot figure out how to forward declare a class
that is in a namespace or nested in another class.

You can't forward declare a nested class.

For a class in a namespace open the namespace, then forward declare
the class, like this to forward declare N::X.

namespace N {
class X;
}



Quote:
class StockTable {
public:
class Entry { ... };
class Key { ... };

Primary Question:

Is it possible to forward declare Entry or C,
without including the definition of StockTable
or A?

Not to my knowledge. You can make a non-nested class (though it could
be in a namespace) StockTableEntry, and inside class StockTable
typedef StockTableEntry to Entry.

You can use void* but it's not recommended because it is not type
safe. That is, in functions expecting a (StockTable::Entry *) expect
a (void *), and in the function body use static_cast to cast from void
* to StockTable::Entry *.



Quote:
Is there a good reason not to allow forward
declarations of the form "class A::C;"?

The class might be private, in which case clients should not be able
to form a pointer to it.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.