0

What is the meaning of this code:

using namespace std;


enum DRAM_Controller { dram_controller_maximum = 10};

void myprint(DRAM_Controller dct)
{
    printf("dct value is: %d\n", dct);
}

int main ()
{

    DRAM_Controller(0); //**--> What is the meaing of this**

    printf("value is : %d\n", dram_controller_maximum);
    printf("value is : %d\n", DRAM_Controller(1));
    myprint(DRAM_Controller(0));
}

The output is: value is : 10 value is : 1 dct value is: 0

Robocop
  • 23
  • 3

4 Answers4

4

It's a cast from int to DRAM_Controller

It creates a temporary of type DRAM_Controller and has no effect

Andrew
  • 24,218
  • 13
  • 61
  • 90
1

The line you're pointing out,

DRAM_Controller(0);

casts a 0 to DRAM_Controller, then ignores it.

It has the same effect as

0;

and is just as useful.

In C++, any integer that can fit in an enum's size can be cast into that enum.
This is one of the reasons that a default: case is a good idea when you "switch-case" on an enum value.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

Here you assign a dram_controller_maximum to 10, which simply means that each time you write something = dram_controller_maximum, you mean something = 10:

enum DRAM_Controller { dram_controller_maximum = 10};

For the following function, if you pass it a number, it will just print it. If you pass a DRAM_Controller variable, it will evaluate it's value (a number, remember), and print it.

void myprint(DRAM_Controller dct)
{
    printf("dct value is: %d\n", dct);
}

The following line just transforms the integer (0) to DRAM_Controller. This line alone is pretty useless:

DRAM_Controller(0); //**--> What is the meaing of this**

The next three lines will print the dram_controller_maximum value converted to int. Remember, in the beginning we said it's equal to 10, so this will just print 10. All the three lines do the same thing: they try to interpret the DRAM_Controller-type value as an int and print it:

printf("value is : %d\n", dram_controller_maximum);
printf("value is : %d\n", DRAM_Controller(1));
myprint(DRAM_Controller(0));

Basically, an enum is just a bunch of ints that have "names":

C exposes the integer representation of enumeration values directly to the programmer. Integers and enum values can be mixed freely, and all arithmetic operations on enum values are permitted. It is even possible for an enum variable to hold an integer that does not represent any of the enumeration values. In fact, according to the language definition, the above code will define CLUBS, DIAMONDS, HEARTS, and SPADES as constants of type int, which will only be converted (silently) to enum cardsuit if they are stored in a variable of that type

and

C++ has enumeration types that are directly inherited from C's and work mostly like these, except that an enumeration is a real type in C++, giving additional compile-time checking.

from wiki.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • Thank you so much for the explanation. I knew casting something as: (unsigned int *) 0xAABBCCDD; But did not come across something like this. Is it something specific to C++ – Robocop Jul 27 '12 at 04:39
  • Ok, found the answer: http://stackoverflow.com/questions/32168/c-cast-syntax-styles – Robocop Jul 27 '12 at 04:45
0

It creates a temporary, unnamed object of type DRAM_Controller initialised with the given value.

The first line has no effect, since the object is destroyed without using its value. The last two lines use the object to initialise the function arguments.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644