-1

Is this the correct way to typedef an inner class in C++?

class Foo
{
public:
    struct A
    {
        typedef bool Type;
    };

    struct B
    {
        typedef int Type;
    };

    typedef struct Foo::nested;
};

The code compiles under Visual Studio 2008 but I'm not sure if it is indeed a typedef of a nested class, or whether the standard permits it.

Olumide
  • 5,397
  • 10
  • 55
  • 104

1 Answers1

3

Not sure what you are after. If you'd like to typedef one of the existing nested structures, try:

typedef struct Foo::A nested;

Most likely you want to place the typedef outside of the Foo struct in this case -- you can already access A from within.

If you wanted to create a typedef inside Foo for a nested type of another struct, you could do this:

typedef struct Bar::nested myNested;

Then you can refer to it as Foo::myNested from outside as well, as long as you declared the typedef public.

Attila
  • 28,265
  • 3
  • 46
  • 55
  • If possible, I'm trying create a typedef for the inner classes of a given class -- just like it is possible to have a typedef for all methods of a class. – Olumide Apr 13 '12 at 15:55
  • You cannot typedef a member function. Other than that, what I wrote does exactly what you say you need. You could write `typedef Foo::A::Type AType;` as well if that's what you want – Attila Apr 13 '12 at 15:59
  • Thanks. I meant pointers to member functions, not member functions. I had the hair-brained idea of collecting all the inner classes in a map and using a key in order to retrieve them. – Olumide Apr 13 '12 at 16:10
  • You cannot access the _types_ at runtime, so unless you are doing some template magic, your map idea will not work (you could use `typeid` to access information about the type at runtime, if that's what you want) – Attila Apr 13 '12 at 16:14