Topic: prototype on nested class
Author: "Richard Smith" <richard@ex-parrot.com>
Date: Mon, 11 Mar 2002 17:40:31 GMT Raw View
"Izidor" <igor@NOobrazSPAM.net> wrote in message
news:a5rjmi$hin$1@wanadoo.fr...
>
> Hi,
>
> such code :
> class a;
> a *o;
>
> is okey in C++
>
> however :
> class a::b;
> a::b *o;
I agree that this is something that would be very useful to be able to do;
however, there are reasons why this isn't currently legal. First, and more
trivially, 'a' is undeclared. How does the compiler know whether it is a
namespace or class? If you do
class a;
class a::b;
a::b *o;
this problem would go away. The main problem, however, is access control.
If the class is defined
class a
{
private:
class b { /* ... */ };
};
declaring class a::b; allows you to use the private name a::b which in the
current language definition you cannot do. One solution to this is to
mangle the access specifier into the name of a member class / function, and
then the declaration a::b would only match a public member class 'b' in
class 'a'.
Unfortunately, there is then the issue of friendship: the code
// Translation unit 1
void foo()
{
class a;
class a::b;
a::b *o;
}
// Translation unit 2
class a
{
private:
friend void foo();
class b { /* ... */ };
};
should probably be legal, but if the friendship declaration is not visible
in translation unit 1 how does the compiler know that a::b is a private
member rather than a public one? Maybe the declaration of a private nested
class could be something like
class a;
class a::private b; // In analogy with a::template b<int>;
although this seems inelegant, both syntactically and semantically for code
to need to know whether they are accessing a private member via a friend
declaration or a public member.
One thing that does seem important however, is that the language does not
allow the definition of a member class of an incomplete type. So for
example, the following code should not be legal:
class a;
class a::b
{
// ...
};
or suddenly anyone can inject arbitrary code into a class: what is to stop
me from writing the above, perhaps in error, when the name a::b does not
exist. Would the compiler be expected to diagnose that as an error? If so,
how? And if it were legal to inject names like this into a class, this
would have "interesting" effects on name look-up in classes. This seems
like a can of worms best left unopened.
--
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.research.att.com/~austern/csc/faq.html ]
Author: "Izidor" <igor@NOobrazSPAM.net>
Date: Wed, 6 Mar 2002 04:37:04 GMT Raw View
Yes, absolutely. So "class a::b" being a class like "class a" it should work
on the same way. But it doesn't. The only reason is maybe because the
compiler need to know whether "b" is nested in public or not. Otherwhise
there is no logical reason, if "class a::b" doesn't exist, it has to generate
a link error not a compile error exactly as with "class a".
Izidor.
<res0oqmv@verizon.net> a crit dans le message de news: 3c83e9af.887738450@News.Verizon.net...
> I think because a forward class declaration should only be used when
> dealing with a pointer or reference value of a class and and they are
> not dereferenced. This is the case in many header file prototypes and
> self-referencing structure nodes declarations. The logic behind this
> is that pointers are all the same size and no furthur information is
> needed when not accessing the memory they point to.
>
> You need to include the class defintiion to use the members of a
> class.
>
>
> On Sun, 3 Mar 2002 00:01:33 GMT, "Izidor" <igor@NOobrazSPAM.net>
> wrote:
>
> >
> >Hi,
> >
> >such code :
> >class a;
> >a *o;
> >
> >is okey in C++
> >
> >however :
> >class a::b;
> >a::b *o;
> >
> >doesn't work.
> >I indeed need to import the full prototype of a to compile properly.
> >
> >My question is merely to know why. I don't see the logic of C++ beyond this.
> >
> >Thanks before hand.
> >
> >
> >
> >---
> >[ 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 ]
> >
>
> ---
> [ 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 ]
>
---
[ 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 ]
Author: "Izidor" <igor@NOobrazSPAM.net>
Date: Sun, 3 Mar 2002 00:01:33 GMT Raw View
Hi,
such code :
class a;
a *o;
is okey in C++
however :
class a::b;
a::b *o;
doesn't work.
I indeed need to import the full prototype of a to compile properly.
My question is merely to know why. I don't see the logic of C++ beyond this.
Thanks before hand.
---
[ 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 ]
Author: res0oqmv@verizon.net
Date: Mon, 4 Mar 2002 21:46:43 GMT Raw View
I think because a forward class declaration should only be used when
dealing with a pointer or reference value of a class and and they are
not dereferenced. This is the case in many header file prototypes and
self-referencing structure nodes declarations. The logic behind this
is that pointers are all the same size and no furthur information is
needed when not accessing the memory they point to.
You need to include the class defintiion to use the members of a
class.
On Sun, 3 Mar 2002 00:01:33 GMT, "Izidor" <igor@NOobrazSPAM.net>
wrote:
>
>Hi,
>
>such code :
>class a;
>a *o;
>
>is okey in C++
>
>however :
>class a::b;
>a::b *o;
>
>doesn't work.
>I indeed need to import the full prototype of a to compile properly.
>
>My question is merely to know why. I don't see the logic of C++ beyond this.
>
>Thanks before hand.
>
>
>
>---
>[ 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 ]
>
---
[ 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 ]