comparing objects or strings

Oct 2, 2017 at 6:23pm
I was watching a video on udemy and the instructor checks to see if two strings are equal by using ==

​In java they use a .equals method to compare two objects such as strings for equality,when we use == we are checking if both objects in memory point to the same address,


is this different in c++? why didn't he use input.compare(password) == 1 instead of input == password?

if anyone could clear that up for me would be great thanks

Oct 2, 2017 at 6:31pm
when we use == we are checking if both objects in memory point to the same address

No, you are checking if both strings are the same.

why didn't he use input.compare(password) == 1 instead of input == password?

While both methods, the operator== is also a method, do basically the same thing the compare() method returns an int that indicates if the string is less than, greater than, or equal to the string in the parameter. The operator returns a bool telling if the two strings are or are not equal to each other.

Oct 2, 2017 at 6:37pm
Ohh ok I think I get it

the == operator is overloaded for strings so using == works but in Java since you can't overload operators you can't do that in java,

is that right?

thanks jlb
Oct 2, 2017 at 6:51pm
yes, that is correct, you cannot overload java which is one of the top couple of flaws in the language (it makes hard core math development almost impossible).

And yes the C++ string class overloads == (and many other) operators to make it very easy to use.
Oct 2, 2017 at 6:56pm
that makes sense, Java is quite restrictive in what you can do

thanks guys =)
Oct 2, 2017 at 7:01pm
In C++, == is used for logical equivalence: true if the operands have equivalent logical state.
Equality of addresses is used to check two variables refer to the same object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>

template < typename T > void foo( const T& a, const T& b )
{
    const bool equal = a == b ; // true if the value of a is equal to the value of b
                                // a and b need not be the same integer

    const bool same_object = std::addressof(a) == std::addressof(b) ;
    // true if both a and b refer to the same object

   std::cout << std::boolalpha << "equal? " << equal << "  same object? " << same_object << '\n' ;
}

int main()
{
    int a = 5, b = 5, c = 6 ;
    foo( a, a ) ; // equal? true  same object? true
    foo( a, b ) ; // equal? true  same object? false
    foo( a, c ) ; // equal? false  same object? false

    std::string s = "abcd", t = "abcd", u = "abcde" ;
    foo( s, s ) ; // equal? true  same object? true
    foo( s, t ) ; // equal? true  same object? false
    foo( s, u ) ; // equal? false  same object? false
}

http://coliru.stacked-crooked.com/a/02810bf1270ab5bf

Pointer-disadvantaged languages (which also tend to be value-semantics-disadvantaged) do not have this luxury of consistency; in those == for some types means logical equivalence, and for some other types means same object identity.
Oct 2, 2017 at 7:10pm
interesting,

so let's say I was to create a class as you did named foo

and I created 2 objects from this class a and b

if I did bool equal = a == b,

does this test if the addresses are equal?

Oct 2, 2017 at 7:33pm
> so let's say I was to create a class as you did named foo

foo is not a class; it is a function (a function template)

If we do write a class, and we make the class EqualityComparable, we would compare logical equivalence (like Java compares variable of type int).
Last edited on Oct 2, 2017 at 7:33pm
Oct 3, 2017 at 11:20am
great,

so if we write a class and make it equalityComparable we would have to overload the == operator for that class?

thanks
Oct 3, 2017 at 12:07pm

in c++ you can overload some other operator to do the comparison if you really wanted to do that (not recommended as it is confusing)

and you can also do a function like bool compare(a,b) if you wanted to.

the right thing to do most of the time is overload ==, but its not the only way.



Oct 3, 2017 at 1:05pm
EqualityComparable: See http://en.cppreference.com/w/cpp/concept/EqualityComparable

Yes, overload the operator== for the class and make it behave ...
Oct 3, 2017 at 2:27pm
> so if we write a class and make it equalityComparable we would have to overload the == operator for that class?

Yes, keeping these things in mind: for two objects a and b belonging to our class

1. a == b should be a predicate - the result of the expression can be considered to be either true or false
2. a == b should result in true if and only if a and b have logically equivalent values.
3. Principle of least surprise dictates that we should also provide for a != b which should be the negation of a == b

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>

enum system_of_measurement { SI, ARCHAIC, IMPERIAL = ARCHAIC };

struct fluid_volume
{
    static constexpr double LITRES_PER_GALLON = 4.54609 ;
    static constexpr double precision = 0.001 ; // 1 ml (SI) or 1 milli gallon (IMPERIAL)

    double value = 0.0 ;
    system_of_measurement units = SI ;
};

// overloaded operator == (true if a and b are logically equivalent)
inline bool operator== ( fluid_volume a, fluid_volume b )
{
    if( a.units != b.units ) // of one is SI and the other is ARCHAIC, convert ARCHAIC to SI
        ( a.units == ARCHAIC ? a.value : b.value ) *= fluid_volume::LITRES_PER_GALLON ;

    return std::round( std::abs( a.value - b.value ) ) < fluid_volume::precision ;
}

// overloaded operator != ( true if a==b is false )
inline bool operator!= ( fluid_volume a, fluid_volume b ) { return !(a==b) ; }

int main()
{
    fluid_volume x { 1250 } ; // 1.25 Kl (kilolitres)
    fluid_volume y { 274.96156, ARCHAIC } ; // gallons

    std::cout << "x == y ? " << std::boolalpha << ( x == y ) << '\n' ; // true
}
Topic archived. No new replies allowed.