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


Author: Andy <wangsiliang99@gmail.com>
Date: Mon, 14 Dec 2009 11:39:56 CST
Raw View
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-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Miles Bader <miles@gnu.org>
Date: Tue, 15 Dec 2009 11:16:58 CST
Raw View
Andy <wangsiliang99@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.

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





Author: Pavel Minaev <int19h@gmail.com>
Date: Tue, 15 Dec 2009 11:14:57 CST
Raw View
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).



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





Author: Yechezkel Mett <ymett.on.usenet@gmail.com>
Date: Tue, 15 Dec 2009 11:17:54 CST
Raw View
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


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