0

Hi
I tried to use a pointer to point to address of an enumurated constant but this made a compile error.
so can can I know address of an enumurated constant and use it with a pointer?
down is a sample code:

#include<iostream.h>
main()
{


enum Date {mainX=1,mainY};


const int *pPointer;


pPointer=&mainX;


cout<<*pPointer;


return 0;
}

thanks

Edited by Nick Evan: Fixed formatting

2
Contributors
2
Replies
4
Views
13 Years
Discussion Span
Last Post by the_one2003a
0

Hi the_one2003a,

What's the point of pointing to an enumerated constant? I think the key is that an enum is really a constant. An enum statement 'enum Date {mainX=1,mainY}' is almost like one is saying
#define mainX 1
#define mainY 2

An enum is realy just a type. C++ only supports pointers to variables and not pointers to types. If you are interested in pointers to types, the closest king I can think of is my research on using typesafe languages with security features. (http://www.cs.dartmouth.edu/~hawblitz/publish/hawblitzel-space2004.pdf, http://www.cs.dartmouth.edu/~hawblitz/)

Anyways... if you wanted just to get at the value of an enum (not point to it), you could do the following:

#include<iostream.h>
main()
{
  
  enum Date {mainX=1,mainY};
  
  Date *pPointer = new Date;
  
  *pPointer=mainX;
  
  cout<<*pPointer;
  
  // mainX=3; ERROR  See?  enum really is just a constant
  cout<<*pPointer;
  
  return 0;
}

Ed

0

Hi
Thanks Cosi for again your great Help
you are just like an angle in my c++ problems.
I wanted to know this to know does c++ support this type of Pointing
and your answer made my mind clean for this prob.

hope to be able to help you in one of your probs someday.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.