c++ - #define 中的一个点

标签 c++

我有如下类

#include <iostream>

#define Gb MemorySizeUnit.Gb
#define Mb MemorySizeUnit.Mb
#define Kb MemorySizeUnit.Kb

using namespace std;

enum MemorySizeUnit {Gb, Mb, Kb};

class Test
{
  private:
  MemorySizeUnit memSizeUnit;
  int memorySize;

  public:
  void setMemory(const int memSize, MemorySizeUnit unit);
 }

我希望能够做类似的事情

Test test;
test.setMemory(20, Gb);// as opposed to test.setMemory(20, MemorySizeUnit.Gb)

编译器不喜欢#define 中的那个,我有一个“.”

最佳答案

在宏扩展中有一个点并没有错,错误来自这样一个事实,即当在枚举定义中扩展宏时,它会导致枚举定义无效。

enum MemorySizeUnit {Gb, Mb, Kb};

扩展为:

enum MemorySizeUnit {MemorySizeUnit.Gb, MemorySizeUnit.Mb, MemorySizeUnit.Kb};

这不是你想要的。

关于c++ - #define 中的一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11818365/

相关文章:

c++ - 如何定义迭代器类成员的类型以使用 STL 容器方法?

C++ windows.h WriteFile 函数

c++ - 您如何找到整数类型在 C++ 中可以表示的值的范围?

c++ - 概率密度的正交例程

将方法返回值分配给 int 时 C++ 程序崩溃

c++ - 有效地从缓冲区读取值

c++:没有重载函数的实例

使用 Objective-C++ 的 iPhone 快速应用程序切换

c++ - 分配 : *** error for object 0x1029249b0: pointer being freed was not allocated

c++ - 如何围绕任意点旋转一组二维形状