Question

Is it possible to do something like this:

class BaseClass
{
public:
    class NestedClass
    {
    };
};

BaseClass foo;
foo.NestedClass bar;

How to get the type from the instance?

Was it helpful?

Solution

What you need to do is the following:

BaseClass::NestedClass bar;

You cannot avoid using the scope resolution operator, but you can make a typedef as a shorthand in the appropriate scope:

typedef BaseCLass::NestedClass NestedClass;

EDIT: Based on the change in question, if you would like the type from an instance, in c++11 you can use decltype:

decltype(foo)::NestedClass;

but this is pointless as decltype would return BaseClass in this instance.

OTHER TIPS

You use the scope resolution operator along with the base class name:

BaseClass::NestedClass bar;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top