Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] operator ==
QtWS25 Last Chance

[SOLVED] operator ==

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.7k Views
  • 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.
  • I Offline
    I Offline
    I-sty
    wrote on last edited by
    #1

    Hi,

    @bool operator == (const theList & tmp){
    if (this == &tmp){
    std::cout << "true";
    return true;
    }
    else
    {
    std::cout << "false";
    return false;
    }
    }@

    I have this code, with operator ==.
    I debugged but, when the this is equal to &tmp, the loop goes to else. Why?

    Sorry, for my bad English. My first language is Hungarian.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Neutron Stein
      wrote on last edited by
      #2

      are you overriding the == operator in theList class ?

      Never Seen !

      1 Reply Last reply
      0
      • I Offline
        I Offline
        I-sty
        wrote on last edited by
        #3

        [quote author="Neutron Stein" date="1343138503"]are you overriding the == operator in theList class ?[/quote]

        Yes. This code is part of the class theList{ ... }

        This is one screenshoot.
        !http://kepfeltoltes.hu/120724/qt3_www.kepfeltoltes.hu_.png(qt)!

        On the image looks that this and tmp are same. But the statement will false, always.

        Sorry, for my bad English. My first language is Hungarian.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [quote author="I-sty" date="1343134196"]
          @bool operator == (const theList & tmp){
          if (this == &tmp){
          @
          [/quote]

          You are comparing the pointers. Do you like to compare the content?

          Then you need to compare the members individually.
          E.g.

          @class A
          {
          int a;
          int b;
          public:
          bool operator== ( const A & rhs)
          {
          return a == rhs.a && b ==rhs.b;
          }
          };
          @
          or similar. Otherwise you would need already the operator you are trying to define.

          NOTE: not tested just typed.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            I-sty
            wrote on last edited by
            #5

            [quote author="koahnig" date="1343139525"][quote author="I-sty" date="1343134196"]
            @bool operator == (const theList & tmp){
            if (this == &tmp){
            @
            [/quote]

            You are comparing the pointers. Do you like to compare the content?
            [/quote]

            Yes, I do.

            Sorry, for my bad English. My first language is Hungarian.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Thanatos.jsse
              wrote on last edited by
              #6

              When compare the this pointer you making a comparison between memory allocations not objects.

              @theList *p_1 = new theList();
              theList *p_2;

              p_2 = p_1;

              if (p_1 == p_2)
              cout << "Are same allocation";@

              BR,

              1 Reply Last reply
              0
              • I Offline
                I Offline
                I-sty
                wrote on last edited by
                #7

                [quote author="koahnig" date="1343139525"][quote author="I-sty" date="1343134196"]
                @bool operator == (const theList & tmp){
                if (this == &tmp){
                @
                [/quote]

                You are comparing the pointers. Do you like to compare the content?

                Then you need to compare the members individually.
                E.g.

                @class A
                {
                int a;
                int b;
                public:
                bool operator== ( const A & rhs)
                {
                return a == rhs.a && b ==rhs.b;
                }
                };
                @
                or similar. Otherwise you would need already the operator you are trying to define.

                NOTE: not tested just typed.

                [/quote]

                Thanks very much koahnig.
                You solved my problem. It's working now.

                Sorry, for my bad English. My first language is Hungarian.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved