User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,629 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2134 | Replies: 2
Reply
Join Date: Aug 2004
Posts: 13
Reputation: the_one2003a is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
the_one2003a the_one2003a is offline Offline
Newbie Poster

Why can't I use Pointers to point to a Enumurated Constant

  #1  
Aug 16th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: Why can't I use Pointers to point to a Enumurated Constant

  #2  
Aug 16th, 2004
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/~hawblit...-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
Reply With Quote  
Join Date: Aug 2004
Posts: 13
Reputation: the_one2003a is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
the_one2003a the_one2003a is offline Offline
Newbie Poster

Re: Why can't I use Pointers to point to a Enumurated Constant

  #3  
Aug 17th, 2004
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC