Topic: forward declaration
Author: theghoul999@yahoo.com (fa)
Date: Mon, 28 Jul 2003 16:55:14 +0000 (UTC) Raw View
hi all,
following the standard what is allowed in forward declarations
beetween:
1) class a_class; //forward declaration
class another_class
{
a_class object;
};
2) class a_class
class another_class
{
a_class* object
}
3) class a_class; //forward declaration
class another_class : public a_class
{
};
it seems on my compiler that just the 2nd one does compile.
more, what the standard says about the standard library? at my
knolewdge with members of the standard librery forward declarations
are not permitted, isn'it?
thanks
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: richard@ex-parrot.com (Richard Smith)
Date: Mon, 28 Jul 2003 19:08:46 +0000 (UTC) Raw View
fa wrote:
> 1) class a_class; //forward declaration
> class another_class
> {
> a_class object;
> };
This is not legal. All members must be complete.
> 2) class a_class
> class another_class
> {
> a_class* object
> }
This is perfectly OK.
> 3) class a_class; //forward declaration
> class another_class : public a_class
> {
> };
Again, this is not legal. Base classes must also be
complete.
> what the standard says about the standard library? at my
> knolewdge with members of the standard librery forward declarations
> are not permitted, isn'it?
I'm not sure I understand the question. If you're asking
whether you are allowed to forward declare classes that are
defined in the standard library, then the answer is no.
--
Richard Smith
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: James Dennett <james@evtechnology.com>
Date: Wed, 1 Nov 2000 23:39:47 GMT Raw View
Ionut Burete wrote:
>
> class A::Impl;
>
> Is this forward declaration allowed in C++ standard?
>
No.
If A is a namespace, you have to use the namespace syntax
namespace A { class Impl; }
If A is a class, you can't forward declare its members.
For more information, refer to the very recent thread (either here or on
comp.lang.c++.moderated, I don't recall which) on this subject.
-- James Dennett <jdennett@acm.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Petru Marginean" <margineanp@plural.com>
Date: Thu, 2 Nov 2000 05:15:50 GMT Raw View
The forward declaration ARE allowed in C++ in the same manner as the static
member and the static functions are handled:
/*A.h*/
class A
{
class B;
//...
};
/*A.cpp*/
class A::B
{
};
/*etc*/
You can read about this in "The Design and Evolution of C++", pg. 289
(Bjarne Stroustrup).
I hope this helps,
Petru
"Ionut Burete" <iburete@real.com> wrote in message
news:8tpq11$nr1nf$1@ID-36702.news.dfncis.de...
> class A::Impl;
>
> Is this forward declaration allowed in C++ standard?
>
> Let's say I have a inner class Impl for a given class A.
> In a header declaration of a different class B, that doesn't see A's
header
> declaration,
> I need to make A::Impl a friend of B and this doesn't work with my copiler
> (MSVC 6.00)
>
> file://A.h
> class A
> {
> class Impl;
> };
> // end of A.h
>
> // B.h
> class B
> {
> public:
> ...
> private:
> ...
> friend class A::Impl; // compiler error
> };
>
> // neither if say
> class A::Impl; // my compiler says A is not a namespace -
> file://end of B.h
>
> // Can I fix this? Is this forward declaration allowed in C++?
> Thank you!
>
>
>
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
> [ Note that the FAQ URL has changed! Please update your bookmarks. ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: rado42 <rado42@my-deja.com>
Date: Thu, 2 Nov 2000 14:52:15 GMT Raw View
In article <8tqm6f$nurrj$1@ID-14233.news.dfncis.de>,
"Petru Marginean" <margineanp@plural.com> wrote:
> The forward declaration ARE allowed in C++ in the same manner as the
static
> member and the static functions are handled:
>
> /*A.h*/
> class A
> {
> class B;
> //...
> };
>
> /*A.cpp*/
>
> class A::B
> {
> };
>
> /*etc*/
>
> You can read about this in "The Design and Evolution of C++", pg. 289
> (Bjarne Stroustrup).
>
> I hope this helps,
> Petru
>
While on this tune, does anybody know why e.g. this fails to compile:
class X;
typedef int Y; // << compilation error
class Y{};
typedef Y X; // << here too
typename XX; // << this too, unless in template
What I am trying to achieve is obviously forward declaring types
that are not just classes, e.g. in order to use them as pointers.
I would find it very usefull e.g. when using template classes
(which, even if used as pointers, require including the full
template definition).
My expectations were that once the keyword 'typename' was introduced,
it could be easily be used in all contexts and not just templates.
It would simplify its definition and make possible forward declarations
of any type (e.g. template instantiations), and not just classes.
Or does anybody know some another workaround for forward declarations
of templates different than having a 'template forward' header file
like <iosfwd>?
Radoslav Getov
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Dennett <james@evtechnology.com>
Date: Thu, 2 Nov 2000 17:12:29 GMT Raw View
rado42 wrote:
>
> While on this tune, does anybody know why e.g. this fails to compile:
>
> class X;
> typedef int Y; // << compilation error
Doesn't give a compilation error for me. Nor should it.
> class Y{};
> typedef Y X; // << here too
Again, that's quite legal. I've just compiled it with
"g++ -ansi -pedantic -Wall" and no warnings/errors.
> typename XX; // << this too, unless in template
Even in a template, that makes no sense in isolation. Its meaning, when
it has meaning, is the same as "XX" would be in non-template code; it is
only used to allow the compiler to assume that the following name really
is a typename when it depends on template parameters.
> What I am trying to achieve is obviously forward declaring types
> that are not just classes, e.g. in order to use them as pointers.
The standard response is "you can't do that," with the justificiation
being that pointers don't all have to be the same size. Pointers to
objects of class type are all required to be the same size, so it is
legal to forward declare classes. It's quite possible for a (char *)
to be larger than a (class X *), e.g., on machines which naturally
address memory in units bigger than a (char *).
Not that I know of any machines where sizeof(char *) > sizeof(class X *).
> I would find it very usefull e.g. when using template classes
> (which, even if used as pointers, require including the full
> template definition).
>
> My expectations were that once the keyword 'typename' was introduced,
> it could be easily be used in all contexts and not just templates.
> It would simplify its definition and make possible forward declarations
> of any type (e.g. template instantiations), and not just classes.
The reason that's not possible isn't syntactic. It's inherent in the
degree of freedom the C++ Standard gives to implementations.
-- James Dennett <jdennett@acm.org>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: John Kewley <kewley@cscs.ch>
Date: Thu, 2 Nov 2000 18:02:50 GMT Raw View
rado42 wrote:
> ...
> My expectations were that once the keyword 'typename' was introduced,
> it could be easily be used in all contexts and not just templates.
> It would simplify its definition and make possible forward declarations
> of any type (e.g. template instantiations), and not just classes.
>
> Or does anybody know some another workaround for forward declarations
> of templates different than having a 'template forward' header file
> like <iosfwd>?
>
> Radoslav Getov
This is exactly what I was trying to get at with my
recent thread on this matter.
--
John M. Kewley Tel: +41 (0) 91 610 8248
Senior Scientist Fax: +41 (0) 91 610 8282
Swiss Center for Scientific Computing mailto:John.Kewley@cscs.ch
Via Cantonale, CH-6928 Manno, Switzerland http://www.cscs.ch/%7Ekewley/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: wmm@fastdial.net
Date: Thu, 2 Nov 2000 18:49:16 GMT Raw View
In article <3A0191D6.D1D4B018@evtechnology.com>,
James Dennett <james@evtechnology.com> wrote:
> The standard response is "you can't do that," with the justificiation
> being that pointers don't all have to be the same size. Pointers to
> objects of class type are all required to be the same size, so it is
> legal to forward declare classes. It's quite possible for a (char *)
> to be larger than a (class X *), e.g., on machines which naturally
> address memory in units bigger than a (char *).
>
> Not that I know of any machines where sizeof(char *) > sizeof(class X
*).
I don't know of any current machines, but the Prime Computer
400 series (late '70s and early '80s) had that characteristic:
it had 16-bit words, two bytes per word. Pointers to words
were 32 bits, pointers to characters were 48 bits. (If I
remember correctly, you could actually point to individual
bits with the 48-bit pointers, although there were no hardware
instructions that worked on arbitrary bit strings. I believe
we used them, though, in the interface with the runtime
support that handled bit strings and BCD nybbles.)
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 2 Nov 2000 18:54:13 GMT Raw View
rado42 <rado42@my-deja.com> writes:
>While on this tune, does anybody know why e.g. this fails to compile:
>
>class X;
>typedef int Y; // << compilation error
>class Y{};
>typedef Y X; // << here too
>typename XX; // << this too, unless in template
>
>What I am trying to achieve is obviously forward declaring types
>that are not just classes, e.g. in order to use them as pointers.
...
>My expectations were that once the keyword 'typename' was introduced,
>it could be easily be used in all contexts and not just templates.
>It would simplify its definition and make possible forward declarations
>of any type (e.g. template instantiations), and not just classes.
The representation of a pointer type may depend on what it points to.
For example, on word-addressed systems, `char *' often has a
different (and less efficient) representation than `int *'.
If the compiler doesn't know what type `XX' is, then it won't
necessarily know what representation to use for `XX *'.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Ionut Burete" <iburete@real.com>
Date: Wed, 1 Nov 2000 19:25:06 GMT Raw View
class A::Impl;
Is this forward declaration allowed in C++ standard?
Let's say I have a inner class Impl for a given class A.
In a header declaration of a different class B, that doesn't see A's header
declaration,
I need to make A::Impl a friend of B and this doesn't work with my copiler
(MSVC 6.00)
file://A.h
class A
{
class Impl;
};
// end of A.h
// B.h
class B
{
public:
...
private:
...
friend class A::Impl; // compiler error
};
// neither if say
class A::Impl; // my compiler says A is not a namespace -
file://end of B.h
// Can I fix this? Is this forward declaration allowed in C++?
Thank you!
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]