Define enum. I use struct {Enum type {};};

Source: Internet
Author: User

This article only discusses how to define a more readable Enum in solidmcp, which is also a side effect of cracking clean code and readable code.

-- Piaoger


> Several methods for defining Enum

In fact, the main difference between these forms is:

Whether the enum definition is placed in a nested Enum type;

Whether the enum type is nested in struct or namespace.

1. No nesting

enum DateTimeKind
{
kUnspecified = 0,
kLocalTime = 1,
kUtcTime = 2
};

inline bool isUtc(DateTimeKind kind)
{
return kind == kUtcTime;
}

2. The Enum type is nested in the namespace.

namespace DateTimeKind
{
enum Type
{
kUnspecified = 0,
kLocalTime = 1,
kUtcTime = 2
};
}

inline bool isUtc(DateTimeKind::Type kind)
{
return kind == DateTimeKind::kUtcTime;
}

3. The Enum type is nested in struct.

struct DateTimeKind
{
enum Type
{
kUnspecified = 0,
kLocalTime = 1,
kUtcTime = 2
};
}

inline bool isUtc(DateTimeKind::Type kind)
{
return kind == DateTimeKind::kUtcTime;
}

4. The Enum type in the class is nested in struct.

No namespace is allowed in the class, so Enum type can only be placed in struct

class DateTime
{
public:
struct Kind
{
enum Type
{
kUnspecified = 0,
kLocalTime = 1,
kUtcTime = 2
};
};

private:
// Data Members
};

inline bool isUtc(DateTime::Kind::Type kind)
{
return kind == DateTime::Kind::kUtcTime;
}

5. No nested Enum of the class

class DateTime
{
public:
enum Kind
{
kUnspecified = 0,
kLocalTime = 1,
kUtcTime = 2
};
};

inline bool isUtc(DateTime::Kind kind)
{
return kind == DateTime::kUtcTime;
}

 

> Nested or not nested?

Relatively speaking, piaoger prefers the nested form, which reduces the chance of name conflict and is more readable.

In terms of readability, datetime: kind: kutctime must be greater than datetime: kutctime. We can clearly see that kutctime is time kind.

To reduce name conflicts, when you want to add a kunknown to multiple Enum in a class, you will find the problem. Generally, you can only add some suffixes as follows to show the difference:

class ClassName
{
public:
enum EnumXX
{
kUnknownXX = 0,
kValue1XX = 1,
kValue2XX = 2
};

enum EnumYY
{
kUnknownYY = 0,
kValue1YY = 1,
kValue2YY = 2
};
};

> Struct or namespace

The weakness of namespace is that it cannot be inherited and cannot be used in class or struct. In struct form, some silly compilers may secretly generate code in struct, such as the default constructor.

Piaoger chose struct {Enum type {};} because this silly compiler is rare and the namespace format is difficult to unify, that is, the third and fourth forms mentioned above.


> Try to put the commonly used Enum out of class for definition.

This can also avoid unnecessary include (although Enum can also be declared forward ).

Qt defines a large number of common Enum in qnamespace. h. (.. \ SRC \ corelib \ Global \ qnamespace. h)

I prefer to add xxxtypes. h to each module and put some related Enum definitions and class forward declarations in this file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.