Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何命名类、结构、枚举和联合_C++_Class_Struct_Enums_Naming - Fatal编程技术网

C++ 如何命名类、结构、枚举和联合

C++ 如何命名类、结构、枚举和联合,c++,class,struct,enums,naming,C++,Class,Struct,Enums,Naming,两个例子: 我要定义时间: class Time { month(){...} year(){...} private: time_t a; } struct Time {int year; int month;} 但C++不能允许定义同名。那么如何定义structname呢?添加前缀或后缀,例如STime或TimeStruct 同样,我想定义颜色: class Color { int color; red(){...} } enu

两个例子:

我要定义时间:

class Time
{
    month(){...} 

    year(){...}

    private:

    time_t a;
}

struct Time    
{int year; int month;}
<>但C++不能允许定义同名。那么如何定义
struct
name呢?添加前缀或后缀,例如
STime
TimeStruct

同样,我想定义颜色:

class Color
{
  int color;

  red(){...}
}

enum Color
{
    e_red,

    e_green
}
也存在名称冲突。那么如何定义
enum
name呢?添加前缀或后缀,例如
EColor
ColorEnum

使用
union
时也存在名称冲突。那么如何避免
struct
enum
union
名称与类名冲突呢?添加前缀或后缀?

您可以使用限制范围

namespace MyClass
{
   class Time { ... };
}

namespace MyStruct
{
  struct Time { ... };
}

...
MyClass::Time c;
MyStruct::Time s;
...

不能理解你的问题…你能解释你想做什么吗?同一个名字不应该同时适用于简单的数据持有者和复杂的类。不管怎样,C++有一个叫做命名空间的东西。如果你想用两个不同的名字命名同一个名字,你就没有概念化程序。好,我会问另一个问题来替换它。