I almost understand, a little more advicie please.
-
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?
-
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?
Have you implemented the operator? logical operator ! (logical x) { ... return ... ; } If not, you will have of course a linker error! Hardy
-
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?
First: an
enum
autoconverts inint
, !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 likelogical 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:
-
First: an
enum
autoconverts inint
, !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 likelogical 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:
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 }