Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I almost understand, a little more advicie please.

I almost understand, a little more advicie please.

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mcgahanfl
    wrote on last edited by
    #1

    Given: Write the following code and discuss what exactly results. 1. enum logical { no, maybe, yes }; 2. logical operator ! (logical x); ------- So this gives me a set called logical. An I can define a variable like logical x(no); Item 2 is a template that takes x and returns NOT X. It is my opinion the template is meaningless. Not FALSE is TRUE, but NOT MAYBE is NO and NOT YES is also NO. Not NO is undefined. Anyway, how can I use this template? x = !(x); Will just yeild a linker error? How do I define a usage of item #2?

    H E 2 Replies Last reply
    0
    • M mcgahanfl

      Given: Write the following code and discuss what exactly results. 1. enum logical { no, maybe, yes }; 2. logical operator ! (logical x); ------- So this gives me a set called logical. An I can define a variable like logical x(no); Item 2 is a template that takes x and returns NOT X. It is my opinion the template is meaningless. Not FALSE is TRUE, but NOT MAYBE is NO and NOT YES is also NO. Not NO is undefined. Anyway, how can I use this template? x = !(x); Will just yeild a linker error? How do I define a usage of item #2?

      H Offline
      H Offline
      Hardy_Smith
      wrote on last edited by
      #2

      Have you implemented the operator? logical operator ! (logical x) { ... return ... ; } If not, you will have of course a linker error! Hardy

      1 Reply Last reply
      0
      • M mcgahanfl

        Given: Write the following code and discuss what exactly results. 1. enum logical { no, maybe, yes }; 2. logical operator ! (logical x); ------- So this gives me a set called logical. An I can define a variable like logical x(no); Item 2 is a template that takes x and returns NOT X. It is my opinion the template is meaningless. Not FALSE is TRUE, but NOT MAYBE is NO and NOT YES is also NO. Not NO is undefined. Anyway, how can I use this template? x = !(x); Will just yeild a linker error? How do I define a usage of item #2?

        E Offline
        E Offline
        Emilio Garavaglia
        wrote on last edited by
        #3

        First: an enum autoconverts in int, !1 is 0 and !0 is 1, hence - to avoid confusion, it is better use { no, yes, maybe } (maybe = 2) now, if x is yes or no, already works. The problem is !maybe = no (by definitionm in C++). If this is unacceptable, do an operator like

        logical operator!(logical a)
        {
        return (a==maybe)? maybe: (logical)!(int)a;
        }

        please note the casts! (or you go in an infinite recursion !) 2 bugs found. > recompile ... 65534 bugs found. :doh:

        M 1 Reply Last reply
        0
        • E Emilio Garavaglia

          First: an enum autoconverts in int, !1 is 0 and !0 is 1, hence - to avoid confusion, it is better use { no, yes, maybe } (maybe = 2) now, if x is yes or no, already works. The problem is !maybe = no (by definitionm in C++). If this is unacceptable, do an operator like

          logical operator!(logical a)
          {
          return (a==maybe)? maybe: (logical)!(int)a;
          }

          please note the casts! (or you go in an infinite recursion !) 2 bugs found. > recompile ... 65534 bugs found. :doh:

          M Offline
          M Offline
          mcgahanfl
          wrote on last edited by
          #4

          Thank you both. I was not casting properly. My work product follows for anyone concerned. //In C or C++ the following is true //1. !0 is true and 0 is false //2. !1 is false and 1 is true //3. !Any number other than 0 is false and !0 is always true enum logical { no, maybe, yes }; // no = 0 and !no is true // maybe = 1 and !maybe is false // yes = 2 and !yes is the same as !maybe logical operator ! (logical x) { x = (logical)!(int)x; return x; } logical operator &&(logical a, logical b) { logical x = no; if(a &&(int) b) { x = yes; } return x; } void CTestinistuffDlg::OnCancel() { logical x(no); logical a(maybe), b(yes); //the Not operator x = operator!(no); //returns maybe x = operator!(maybe); //returns no x = operator!(yes); //returns no x = operator&&(no,no); //returns no x = operator&&(no,maybe); //returns no x = operator&&(no,yes); //returns no x = operator&&(maybe,maybe); //returns yes x = operator&&(maybe,no); //returns no x = operator&&(maybe,yes); //returns yes x = operator&&(yes,yes); //returns yes x = operator&&(yes,no); //returns no x = operator&&(yes,maybe); //returns yes }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups