
enum/c++/ms visual C++.net
Quote:
> No answer so I guess that was not a logical question?
See if this program answers your question.
#include <iostream>
using std::cout;
using std::endl;
enum direction { north, south, east, west };
void facing(direction d) {
switch (d) {
case north: {
cout << "North" << endl;
break;
}
case south: {
cout << "South" << endl;
break;
}
case east: {
cout << "East" << endl;
break;
}
case west: {
cout << "West" << endl;
break;
}
}
}
int main() {
direction x = north;
facing(x);
facing(west);
}
Quote:
> second question that I need someone to read.
> why is passing by pointer bad?
It isn't. Passing a pointer allows you to do several things like (1) change
the value being pointed at, and (2) avoid a copy construction which improves
efficiency. If you are passing a pointer to achieve either of these, it is
generally better to use a reference (&) instead. Passing by pointer verses
not passing by pointer will likely not make must of difference in the
program size. In many cases, I would expect it to make no difference at all.
From an ealier message, you asked:
Quote:
> would it work better possible to create a pointer that targets the
> enum for incrimination purposes inside the function.
> e.g. *facingPtr++
Incrementing a pointer like this is intended for an array. Say you had done
the following:
direction arr[4] = {north, east, south, west};
direction* facingPtr = &arr[0]; // points to north in arr
facing(*facingPtr++); // prints east
If you are using C++, I'd suggest using a std::vector and an iterator
instead. You're less likely to make type correctness mistakes that way.
Quote:
> can you increment an enumeration?
No, the operator++ is not defined by default for an enumeration. You can
define your own of course.
Cheerio!
--
Brandon Bray Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.