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?

有帮助吗?

解决方案

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.

其他提示

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

BaseClass::NestedClass bar;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top