Similarities between enumerations and classes (C++)

I read about the “enum hack” (here is a very complicated case of that) and I wanted to try it. So I coded the following simple C++ program:

class Something {
  enum{c1 = 5, c2 = 7}
  static const int v1 = c1;
  int v2[c2];
 public:
  Something() {
    std::cout << v1 << " " << v2 << std::endl;
  }
};

You are right, it isn’t valid. There is a typo after the enum, I forgot the semicolon. But I found the error message of the compiler a bit confusing: two or more data types in declaration of v1. I thought about a bit and after than this error message helped me to understand better why is semicolon so important.

In the C++ language, there is an optional declarator part after the body of the enum. The position of the semicolon can indicate that “there is no declarator” and when the semicolon is missing, then the next declaration will be the declarator part of the enum! The following code is a valid C++ code, the v1 is the declarated with the declarator of the enum, he is a static enumeration constant and his constant value is c1:

class Something {
  enum{c1 = 5, c2 = 7}
  static const v1 = c1;
  int v2[c2];

 public:
  Something() {
    std::cout << v1 << " " << v2 << std::endl;
  }
};

The first note, that definition of classes and definition of enumerations have similar syntax (keyword – body – declarator). Of course there are also differences: the body of an enumeration is always a brace-enclosed initializatior list and the separated declaration/definition of the class members is strongly encouraged. The second note, that we can use the static and the const keywords in the class definition (because of the declarator part) even when it is impossible to add a storage class specifier to a user-defined class type.

Example1: Enumeration definition with declarator.

enum Members {
  Jim = 10,
  Joe = 20,
  Sam = 30
} static const chief = Sam;

Example2: Variable declaration with a user-defined type (it is similar to “static const int something;”)

static const enum Members {
  Jim = 10,
  Joe = 20,
  Sam = 30
} chief = Sam;

Example3: Class definition with declarator.

class Something {
  enum {siz = 5};
  int data;
  static const int arr[siz];
 public:
  Something(int = 0);
  operator int() const;
} static const s1 = 333;

Something::Something(int d): data(d) {}

Something::operator int() const {
  return arr[data % siz];
}

const int Something::arr[] = {73,11,54,22,8};

Example4: Variable declaration with user-defined class type:

static const class Something {
  enum {siz = 5};
  int data;
  static const int arr[siz];
 public:
  Something(int = 0);
  operator int() const;
} s1 = 333;

Something::Something(int d): data(d) {}

Something::operator int() const {
  return arr[data % siz];
}

const int Something::arr[] = {73,11,54,22,8};

The syntax seems as weird as usually. Coding in C++ like riding wild mustangs. But at least the today’s conclusion is very simple: try not to forget the semiciolons and the declarators.

Advertisements

About vrichard86

I'm Richard and I'm an enthusiastic coder. I want to be better and I think a professional journal is a great self improvement tool. Welcome to my blog, to my hq, to my home on the web!
This entry was posted in C++ and tagged , , . Bookmark the permalink.