Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can we distinguish between data member and nested class in a normal(non-template) class?

0 views
Skip to first unread message

Andy

unread,
Dec 14, 2009, 12:39:56 PM12/14/09
to
class Container
{
public:
static int X;
class X
{
public:
X()
{
std::cout << "X ctor" << std::endl;
}
};
};

void g()
{
// How can we access type Container::X here, since we cannot use
typename in normal class?
}

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Miles Bader

unread,
Dec 15, 2009, 12:16:58 PM12/15/09
to
Andy <wangsi...@gmail.com> writes:

> class Container
> {
> public:
> static int X;
> class X

...


>
> How can we access type Container::X here, since we cannot use
> typename in normal class?

You can use "class Container::X", e.g.:

void g()
{
Container c;
class Container::X x;
c.X = 3;
}

-Miles

--
Inhumanity, n. One of the signal and characteristic qualities of humanity.

Pavel Minaev

unread,
Dec 15, 2009, 12:14:57 PM12/15/09
to
On Dec 14, 9:39 am, Andy <wangsilian...@gmail.com> wrote:
> class Container
> {
> public:
> static int X;
> class X
> {
> public:
> X()
> {
> std::cout << "X ctor" << std::endl;
> }
> };
>
> };
>
> void g()
> {
> // How can we access type Container::X here, since we cannot use
> typename in normal class?
>
> }

Same as for similar case with namespace scope:

class C::X foo; // C::X is type here
int n = C::X; // C::X is static member here

Note that this won't compile in the first place:

class C {
static int X;
typedef int X;
};

so there's no issue with disambiguation in that case (where you
couldn't use class/struct/union tag).

Yechezkel Mett

unread,
Dec 15, 2009, 12:17:54 PM12/15/09
to
On Dec 14, 7:39 pm, Andy <wangsilian...@gmail.com> wrote:
> class Container
> {
> public:
> static int X;
> class X
> {
> public:
> X()
> {
> std::cout << "X ctor" << std::endl;
> }
> };
>
> };
>
> void g()
> {
> // How can we access type Container::X here, since we cannot use
> typename in normal class?
>
> }

You are only allowed to use the name X for both the variable and the
class due to a backwards compatibility rule with C where the struct
names live in a different namespace to other names.

In other words what you have is equivalent to the following:

int X;
struct X;

The rule in this case is that the struct name is hidden. You can
explicitly refer to it with struct X or class X; in your case class
Container::X. And as a general rule, don't do that.

Yechezkel Mett

0 new messages