嵌套类型:struct vs class

Ita*_*ham 0 c++ inner-classes c++11

我应该何时在类中的结构中定义成员,何时应该在嵌套类中定义成员?

例如:

class SkypeProtocol
{
public:
    SkypeProtocol();
    virtual ~SkypeProtocol(){}

private:

    class SkypeProtocolDateTime
    {
    private:
        UI32 uDate;
        ERROR GetDateString(PUCHAR pcBuffer,PUI32 uBufLen);
    };

};
Run Code Online (Sandbox Code Playgroud)

要么

class SkypeProtocol
{
private:
    SkypeProtocol();
    virtual ~SkypeProtocol(){}

    typedef struct SkypeProtocolDateTime
    {
    private:
        UI32 uDate;
        ERROR GetDateString(PUCHAR pcBuffer,PUI32 uBufLen);
    }SSKYPE_STRUCT;

};
Run Code Online (Sandbox Code Playgroud)

Hco*_*org 6

在C++中,除了成员的默认可见性(类为private,结构为public)之外,类和结构之间没有区别.

如果您的对象仅用于存储(没有很多逻辑实现),那么通常的做法是将其创建为结构,并仅将类用于更复杂的对象.

BTW:在C++中你不需要使用typedef struct {...} StructName;符号,简单struct StructName {...};就足够了.