equality

Why isn't (“Maya” == “Maya”) true in C++?

雨燕双飞 提交于 2019-11-29 02:30:35
问题 Any idea why I get "Maya is not Maya" as a result of this code? if ("Maya" == "Maya") printf("Maya is Maya \n"); else printf("Maya is not Maya \n"); 回答1: Because you are actually comparing two pointers - use e.g. one of the following instead: if (std::string("Maya") == "Maya") { /* ... */ } if (std::strcmp("Maya", "Maya") == 0) { /* ... */ } This is because C++03, §2.13.4 says: An ordinary string literal has type “array of n const char ” ... and in your case a conversion to pointer applies.

C++ object equality

谁都会走 提交于 2019-11-29 02:14:09
I have a class MyCloth and one one object instance of that class that I instantiated like this: MyCloth** cloth1; And at one point in the program, I will do something like this: MyCloth** cloth2 = cloth1; And then at some point later, I want to check to see if cloth1 and cloth2 are the same. (Something like object equality in Java, only here, MyCloth is a very complex class and I can''t build an isEqual function.) How can I do this equality check? I was thinking maybe checking if they point to the same addresses. Is that a good idea? If so, how do I do that? You can test for object identity by

Why does new String('hello') === new String('hello') evaluate to False? [duplicate]

喜欢而已 提交于 2019-11-29 02:04:53
问题 This question already has an answer here: Which equals operator (== vs ===) should be used in JavaScript comparisons? 49 answers JavaScript comparison operators: Identity vs. Equality 4 answers Why does the following statement return false in JavaScript? new String('hello') === new String('hello') 回答1: Two String objects will always be unequal to each other. Note that JavaScript has string primitive values as well as a String constructor to create wrapper objects. All object equality

Bug with equals operator and NSObjects in Swift 2.0?

心已入冬 提交于 2019-11-29 01:57:57
Ok, something strange is happening when writing your own equals operator for NSObject subclasses in Swift 2.0 like this: func ==(lhs: MyObject, rhs: MyObject) -> Bool { return lhs.identifier == rhs.identifier } For a class that looks like this: class MyObject: NSObject { let identifier: String init(identifier: String) { self.identifier = identifier } } This used to work just fine in Swift 1.2 and below. It still kind of works: let myObject1 = MyObject(identifier: "A") let myObject2 = MyObject(identifier: "A") let result = (myObject1 == myObject2) // result is true So far so good, but what if

Override == operator in Ruby

不羁岁月 提交于 2019-11-28 22:31:47
According to the docs , Array.include? uses the == comparison on objects. I come from Java where such things are (usually) done with .equals() which is easy to override for a particular object. How can I override == in Ruby to allow me to specify the behaviour of Array.include? for my particular object? Many thanks. In Ruby == is just a method (with some syntax sugar on top allowing you to write foo == bar instead of foo.==(bar) ) and you override == just like you would any other method: class MyClass def ==(other_object) # return true if self is equal to other_object, false otherwise end end

What's the right way to implement equality in ruby

家住魔仙堡 提交于 2019-11-28 21:57:15
问题 For a simple struct-like class: class Tiger attr_accessor :name, :num_stripes end what is the correct way to implement equality correctly, to ensure that == , === , eql? , etc work, and so that instances of the class play nicely in sets, hashes, etc. EDIT Also, what's a nice way to implement equality when you want to compare based on state that's not exposed outside the class? For example: class Lady attr_accessor :name def initialize(age) @age = age end end here I'd like my equality method

Map using tuples or objects

三世轮回 提交于 2019-11-28 18:39:40
I'm trying to use the new (ES6) Map objects in order to represent a map between properties and a value. I have objects in a form similar to: {key1:value1_1,key2:value2_1},..... {key1:value1_N,key2:value2_N} I want to group them based on both their key1 and key2 value. For example, I want to be able to group the following by x and y : [{x:3,y:5,z:3},{x:3,y:4,z:4},{x:3,y:4,z:7},{x:3,y:1,z:1},{x:3,y:5,z:4}] And obtain a Map containing: {x:3,y:5} ==> {x:3,y:5,z:3},{x:3,y:5,z:4} {x:3,y:4} ==> {x:3,y:4,z:4},{x:3,y:4,z:7} {x:3,y:1} ==> {x:3,y:1,z:1} In Python, I'd use tuples as dictionary keys. ES6

Comparing NaN values for equality in Javascript

假装没事ソ 提交于 2019-11-28 18:06:25
I need to compare two numeric values for equality in Javascript. The values may be NaN as well. I've come up with this code: if (val1 == val2 || isNaN(val1) && isNaN(val2)) ... which is working fine, but it looks bloated to me. I would like to make it more concise. Any ideas? Anant Try using Object.is() , it determines whether two values are the same value. Two values are the same if one of the following holds: both undefined both null both true or both false both strings of the same length with the same characters in the same order both the same object both numbers and both +0 both -0 both

Why the compiler emits box instructions to compare instances of a reference type?

柔情痞子 提交于 2019-11-28 17:48:39
Here is a simple generic type with a unique generic parameter constrained to reference types: class A<T> where T : class { public bool F(T r1, T r2) { return r1 == r2; } } The generated IL by csc.exe is : ldarg.1 box !T ldarg.2 box !T ceq So each parameter is boxed before proceeding with the comparison. But if the constraint indicates that "T" should never be a value type, why is the compiler trying to box r1 and r2 ? It's required to satisfy the verifiability constraints for the generated IL. Note that unverifiable doesn't necessarily mean incorrect . It works just fine without the box

bash string equality [duplicate]

两盒软妹~` 提交于 2019-11-28 15:35:31
问题 This question already has an answer here: What is the difference between operator “=” and “==” in Bash? 2 answers In bash , what's the difference, if any, between the equal and double equal test operators? [[ "a" = "a" ]] && echo equal || echo not-equal [[ "a" == "a" ]] && echo equal || echo not-equal [[ "a" = "b" ]] && echo equal || echo not-equal [[ "a" == "b" ]] && echo equal || echo not-equal results in: equal equal not-equal not-equal 回答1: There's no difference, == is a synonym for =