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 426,526 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 1,832 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: 1951 | Replies: 5
Reply
Join Date: Oct 2004
Location: India
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

enum range

  #1  
Sep 24th, 2005
#include<iostream.h>
int main()
{
	enum flag{ x=1,y=2,z=4,e=8};
	flag f2=flag(99); // out of range
	return 0;
}

In this code the range of flag is 0:15 but it still compiles when 99 is entered which is out of range!!
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: enum range

  #2  
Sep 24th, 2005
So? C++ doesn't try to stop you from trying to do stupid things. How about instead of asking why you can do something blatantly wrong, DON'T DO IT TO BEGIN WITH.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Location: India
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: enum range

  #3  
Sep 24th, 2005
Well, I tried this because this was given as an example in Bjarne Stroustrup's book..but I couldn't get the result he specified!
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: enum range

  #4  
Sep 24th, 2005
>I tried this because this was given as an example in Bjarne Stroustrup's book
If you're citing a reference, give us sections and/or page numbers. Luckily, I both have that book and know what part of it you're talking about. It's page 77 in my most recent copy, section 4.8, Enumerations.

>but I couldn't get the result he specified!
You must have missed the comment:
flag f4 = flag(99); // undefined: 99 is not within the range of flag
Undefined means you can do it and the compiler probably won't even warn you, but it's not guaranteed to do anything expected, and there's nothing stopping undefined behavior from doing very real damage. I've heard stories of undefined behavior blowing out CRT monitors and wiping hard drives. Granted, that's an extreme, but the possibility should be more than enough to get you thinking.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: enum range

  #5  
Sep 24th, 2005
c and c++ compilers do not do range checking on enumerations or pointers. It lets you hang yourself with a very very long rope if you want to. Its up to the programmer to validate data ranges and buffer sizes before using or doing something with them. Those are two very good reasons for c++ programs to use the STL classes.
Reply With Quote  
Join Date: Oct 2004
Location: India
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: enum range

  #6  
Sep 25th, 2005
Yes, I meant that very topic from the book. Thanks for the replies!
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
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:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC