enum/c++/ms visual C++.net 
Author Message
 enum/c++/ms visual C++.net

can you pass an enum to a function.
********************************
function header
void facing(direction facing)

call
facing(facing);

prototype
void facing(direction);

declaration
enum direction {north, south, east, west};
direction facing;



Mon, 05 Sep 2005 10:38:43 GMT  
 enum/c++/ms visual C++.net
No reason why you can't. However having your function called "facing" and
your enum variable called "facing" may lead to problems. I am not sure if
the name space for functions and enumerations is completely different. But
otherwise of course you can pass an enumerated value to a function.
Quote:

> can you pass an enum to a function.
> ********************************
> function header
> void facing(direction facing)

> call
> facing(facing);

> prototype
> void facing(direction);

> declaration
> enum direction {north, south, east, west};
> direction facing;



Mon, 05 Sep 2005 09:27:20 GMT  
 enum/c++/ms visual C++.net
it does not allow passing in this way. I will change the name of the
function, but i do not believe this should be an issue. would it work better
possible to create a pointer that targets the enum for incrimination
purposes inside the function.

e.g. *facingPtr++
can you increment an enumeration?
thanks for the help.
peace


Quote:
> No reason why you can't. However having your function called "facing" and
> your enum variable called "facing" may lead to problems. I am not sure if
> the name space for functions and enumerations is completely different. But
> otherwise of course you can pass an enumerated value to a function.


> > can you pass an enum to a function.
> > ********************************
> > function header
> > void facing(direction facing)

> > call
> > facing(facing);

> > prototype
> > void facing(direction);

> > declaration
> > enum direction {north, south, east, west};
> > direction facing;



Tue, 06 Sep 2005 18:24:40 GMT  
 enum/c++/ms visual C++.net
No answer so I guess that was not a logical question?
second question that I need someone to read.
why is passing by pointer bad? I do not feel like returning anything.in my
programs. Isn't the size of hard drives capable of handling a little bigger
.exe do pointers allocate such a massive amount of memory to make void
functions passed with only pointers little in pro vs..s. con?


Quote:
> it does not allow passing in this way. I will change the name of the
> function, but i do not believe this should be an issue. would it work
better
> possible to create a pointer that targets the enum for incrimination
> purposes inside the function.

> e.g. *facingPtr++
> can you increment an enumeration?
> thanks for the help.
> peace



> > No reason why you can't. However having your function called "facing"
and
> > your enum variable called "facing" may lead to problems. I am not sure
if
> > the name space for functions and enumerations is completely different.
But
> > otherwise of course you can pass an enumerated value to a function.


> > > can you pass an enum to a function.
> > > ********************************
> > > function header
> > > void facing(direction facing)

> > > call
> > > facing(facing);

> > > prototype
> > > void facing(direction);

> > > declaration
> > > enum direction {north, south, east, west};
> > > direction facing;



Wed, 07 Sep 2005 15:48:27 GMT  
 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.



Sat, 10 Sep 2005 02:03:36 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Value V.S Reference/C++/MS Visual C++.net

2. Visual C++ .NET coexisting with Visual C++ v6.0 SP5

3. Visual C++ .NET reviews, comparisons to Visual C++ 6.0

4. Visual C++ 6.0 vs. Visual C++ .NET

5. Visual C++ .NET coexisting with Visual C++ v6.0 SP5

6. DoModal Visual C++ 6.0 and Visual C++ .NET?

7. Is MS Visual C++ 4 a good compiler to learn C++ (i'm beginner)

8. Borland C/C++ vs MS Visual C/C++ ?

9. FORTRAN and C/C++ in Microsoft Visual C++ 1.51 and MS PowerStation

10. Diff : MS C++ ver. 7 and Visual C++

11. MS Visual C++ vs Borland C++

12. Newbie Problem with Borland Turbo C++ and MS Visual C++

 

 
Powered by phpBB® Forum Software