locked
C++ enum RRS feed

  • Question

  • I have a enum & struct

    typedef Enum{

    A= 0, B=1, C=2,D=3,E=4,F=5,G=6,H=7,I=8,J=9,K=10} Sample;

    typedef struct{ Sample start, sample end}Mode;

    Mode modeone; modeone.start = A; modeone.end=H;

    Mode modetwo; modetwo.start = E; modetwo.end=K;

    I am trying to find common range between modeone & modetwo.

    Is there any API to find the common range under these scenarios>

    Monday, April 4, 2016 9:01 PM

Answers

  • Check this:

    Sample s = modeone.start > modetwo.start ? modeone.start : modetwo.start;

    Sample e = modeone.end < modetwo.end ? modeone.end : modetwo.end;

    if( s <= e )

    {

           Mode common_range; common_range.start = s; common_range.end = e;

           //...

    }

    else

    {

           // no common range

    }

    • Proposed as answer by Hart Wang Friday, April 15, 2016 8:04 AM
    • Marked as answer by Hart Wang Monday, April 18, 2016 9:43 AM
    Tuesday, April 5, 2016 5:13 AM

All replies

  • 1. I'm not sure this question has anything really to do with enum's. How would it differ if 'Mode' were just a pair of int's?

    2. What is the result supposed to be if there is no common range?

    3. There are a lot of typos in your post; please post actual code.


    David Wilkinson | Visual C++ MVP

    Tuesday, April 5, 2016 1:58 AM
  • Thanks for posting here.

    Sorry but there seems to be not an API can get the common range in Enum Class. But there is Enum::CompareTo Method can help you compare this instance to a specified object and returns an indication of their relative values. If it returns 0, means the value of this instance is equal to the value of target.  

    Tuesday, April 5, 2016 2:01 AM
  • Thanks for posting here.

    Sorry but there seems to be not an API can get the common range in Enum Class. But there is Enum::CompareTo Method can help you compare this instance to a specified object and returns an indication of their relative values. If it returns 0, means the value of this instance is equal to the value of target.  


    This is only for C++/CLI managed enum's.

    David Wilkinson | Visual C++ MVP

    Tuesday, April 5, 2016 2:38 AM
  • Check this:

    Sample s = modeone.start > modetwo.start ? modeone.start : modetwo.start;

    Sample e = modeone.end < modetwo.end ? modeone.end : modetwo.end;

    if( s <= e )

    {

           Mode common_range; common_range.start = s; common_range.end = e;

           //...

    }

    else

    {

           // no common range

    }

    • Proposed as answer by Hart Wang Friday, April 15, 2016 8:04 AM
    • Marked as answer by Hart Wang Monday, April 18, 2016 9:43 AM
    Tuesday, April 5, 2016 5:13 AM