473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Enum and circular definition

enum X { A, B = A } // will compile
enum Y { B = A, A } // won't, claims "circular reference"

That's a bit strange, isn't it?

Eq.
Jun 27 '08 #1
5 1877
enum X { A, B = A } // will compile
enum Y { B = A, A } // won't, claims "circular reference"

That's a bit strange, isn't it?
If you don't specify a value for an enum it uses 1 more than the previous
value (or 0 for the first value), so in your first example A is given the
value 0 (because it's the first value) and then B is given the same value
(because you've specified it's equal to A).

In the second case you're saying B should be equal to A, so it needs to work
out the value of A. But since A has no assigned value and follows B, the
value of A should be B+1. Hence B is defined in terms of A and A is defined
in terms of B - a circular reference.

Chris Jobson
Jun 27 '08 #2
I think the error is that the evalution of the value for B involves a
circular reference. This is because, in case of enum Y { B = A, A } the
evaluation of A depends on what the value of B will be. But then if B = A how
will you evaluate B

However in case enum X { A, B = A } , A can be evaluated independenly and
so can be B after that.
I guess thats the reason.

Sujeet

"Paul E Collins" wrote:
enum X { A, B = A } // will compile
enum Y { B = A, A } // won't, claims "circular reference"

That's a bit strange, isn't it?

Eq.
Jun 27 '08 #3
Paul E Collins wrote:
enum X { A, B = A } // will compile
enum Y { B = A, A } // won't, claims "circular reference"

That's a bit strange, isn't it?
I don't think so.

And why use a forward defined value anyway? Note that { B, A = B }
would be a valid equivalent for it.

--
Rudy Velthuis http://rvelthuis.de

"Raymond's Law of Software: Given a sufficiently large number of
eyeballs, all bugs are shallow." -- Eric S. Raymond
Jun 27 '08 #4
"Chris Jobson" <ch**********@btinternet.comwrote:
In the second case you're saying B should be equal to A, so it needs to
work out the value of A.
Oh yeah, of course. Good point. I bizarrely almost forgot that they have
numeric values, because I never define or use those unless it's a bitmask.

Eq.
Jun 27 '08 #5
On Jun 12, 11:57*pm, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
enum X { A, B = A } // will compile
enum Y { B = A, A } // won't, claims "circular reference"

That's a bit strange, isn't it?

Eq.
I think Chris Jobson explained the reason why it is so.

Is that just a knowledge sharing question or are you trying to solve
some issue?

I am trying to see some usecase for that code. As long as I know
nowadays C# programmers does not need to think enums in terms of
numbers or attached values inside. They are become more abstract.
Those were the good old days of C++, we try comparing the value of
enum with int values in if statements.

-Cnu
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: crichmon | last post by:
Any general advice for dealing with circular dependencies? For example, I have a situation which, when simplified, is similar to: ///////////// // A.h class A { public: int x;
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
2
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
3
by: Jens Müller | last post by:
I have a file here with several enums: #ifndef PLANARSEP_OPTIMIZE_H #define PLANARSEP_OPTIMIZE_H enum fund_cycle_behavior_t {PASS_MODE_FIRST, PASS_MODE_BEST, PASS_MODE_ALL};
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.