Question

Given this enum and assignment:

enum Points {point2d = 2, point2w, point3d = 3, point3w};
Points pt3d = point3d;

At a breakpoint just past this assignment, the debugger reports that pt3d has the value point2w. Why not point3d, as assigned?

Was it helpful?

Solution

An enum is just a way to set fancy, compile time type safe names for enums. What happens is that each of your enum values get assigned a numerical integer value:

enum Points
{
  // Explicitly map point2d to value 2.
  point2d = 2,

  // Let point2w get an automatic mapping - will be previous + 1 = 3.
  point2w,

  // Explicitly map pint3d to value 3.
  point3d = 3,

  // Let point3w get an automatic mapping - will be previous + 1 = 4.
  point3w
}

So to the runtime, the statement Points pt3d = point3d is the same as Points pt3d = 3. When displaying, the debugger simply takes the first enum value mapped againts 3, which is point2w.

OTHER TIPS

enum Points {point2d = 2, point2w, point3d = 3, point3w};
Points pt3d = point3d;

When you don't set a value like for point2w, it continue to increment, then since point2d = 2, point2w will be equal to 3 like point3d. point3w will be equal to 4 because point3d is set to 3.

You should read this about enum.

Hope that helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top