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. can anybody help me out

can anybody help me out

Scheduled Pinned Locked Moved C / C++ / MFC
help
12 Posts 4 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.
  • I Iain Clarke Warrior Programmer

    philiptabraham wrote:

    can anybody help me out

    Not if you don't read the sticky posting called "how to get answers", and the line that says "please use meaningful subjects". I've yet to see someone post a message with a subject of "ignore this - I don't need any assistance". But I'll be nice anyway.. 1/ The numbers are different, so there will be differences. 2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object. If it's CPoint, then it's trivial. If it opens / closes databases, does a network operation etc, then the difference could be HUGE. Iain, ------- Update: See CPallini's and my conversation later. Short version: No actual difference between the two bits of code (except the numbers!)

    modified on Friday, February 01, 2008 5:13:33 AM

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #3

    No, really, it's true...

    Cédric Moonen Software developer
    Charting control [v1.2]

    C I 2 Replies Last reply
    0
    • I Iain Clarke Warrior Programmer

      philiptabraham wrote:

      can anybody help me out

      Not if you don't read the sticky posting called "how to get answers", and the line that says "please use meaningful subjects". I've yet to see someone post a message with a subject of "ignore this - I don't need any assistance". But I'll be nice anyway.. 1/ The numbers are different, so there will be differences. 2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object. If it's CPoint, then it's trivial. If it opens / closes databases, does a network operation etc, then the difference could be HUGE. Iain, ------- Update: See CPallini's and my conversation later. Short version: No actual difference between the two bits of code (except the numbers!)

      modified on Friday, February 01, 2008 5:13:33 AM

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      Iain Clarke wrote:

      2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object.

      I thought the same. But I made a little test (optimization disabled):

      #include using namespace std;

      class MyClass
      {
      public:
      int _a, _b;
      MyClass(){}
      MyClass(int a, int b):_a(a), _b(b){}
      // copy constructor and assignment operator arbitrarly messed up.
      MyClass(const MyClass & proto)
      {
      _a = 0;
      _b = 0;
      }
      MyClass & operator = (const MyClass & proto)
      {
      _a = proto._b;
      _b = proto._a;
      return *this;
      }
      };

      void main()
      {
      MyClass my1(3,2);
      MyClass my2=MyClass(3,2);

      MyClass my3(my1);
      MyClass my4;
      my4=my1;
      
      cout << "my1: " << my1.\_a << " " << my1.\_b << endl;
      cout << "my2: " << my2.\_a << " " << my2.\_b << endl;
      cout << "my3: " << my3.\_a << " " << my3.\_b << endl;
      cout << "my4: " << my4.\_a << " " << my4.\_b << endl;
      

      }

      and to my surprise, the output:

      my1: 3 2
      my2: 3 2
      my3: 0 0
      my4: 2 3

      Well I'm really upset about. What do you think?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      C I 2 Replies Last reply
      0
      • C CPallini

        Iain Clarke wrote:

        2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object.

        I thought the same. But I made a little test (optimization disabled):

        #include using namespace std;

        class MyClass
        {
        public:
        int _a, _b;
        MyClass(){}
        MyClass(int a, int b):_a(a), _b(b){}
        // copy constructor and assignment operator arbitrarly messed up.
        MyClass(const MyClass & proto)
        {
        _a = 0;
        _b = 0;
        }
        MyClass & operator = (const MyClass & proto)
        {
        _a = proto._b;
        _b = proto._a;
        return *this;
        }
        };

        void main()
        {
        MyClass my1(3,2);
        MyClass my2=MyClass(3,2);

        MyClass my3(my1);
        MyClass my4;
        my4=my1;
        
        cout << "my1: " << my1.\_a << " " << my1.\_b << endl;
        cout << "my2: " << my2.\_a << " " << my2.\_b << endl;
        cout << "my3: " << my3.\_a << " " << my3.\_b << endl;
        cout << "my4: " << my4.\_a << " " << my4.\_b << endl;
        

        }

        and to my surprise, the output:

        my1: 3 2
        my2: 3 2
        my3: 0 0
        my4: 2 3

        Well I'm really upset about. What do you think?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        [my articles]

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #5

        It's because the compiler will treat this:

        CMyCLass my1 = my2;

        As this:

        CMyClass my1(my2);

        If you would have done that operation on two lines, then the assignment operator would have been called.

        Cédric Moonen Software developer
        Charting control [v1.2]

        C 1 Reply Last reply
        0
        • C CPallini

          Iain Clarke wrote:

          2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object.

          I thought the same. But I made a little test (optimization disabled):

          #include using namespace std;

          class MyClass
          {
          public:
          int _a, _b;
          MyClass(){}
          MyClass(int a, int b):_a(a), _b(b){}
          // copy constructor and assignment operator arbitrarly messed up.
          MyClass(const MyClass & proto)
          {
          _a = 0;
          _b = 0;
          }
          MyClass & operator = (const MyClass & proto)
          {
          _a = proto._b;
          _b = proto._a;
          return *this;
          }
          };

          void main()
          {
          MyClass my1(3,2);
          MyClass my2=MyClass(3,2);

          MyClass my3(my1);
          MyClass my4;
          my4=my1;
          
          cout << "my1: " << my1.\_a << " " << my1.\_b << endl;
          cout << "my2: " << my2.\_a << " " << my2.\_b << endl;
          cout << "my3: " << my3.\_a << " " << my3.\_b << endl;
          cout << "my4: " << my4.\_a << " " << my4.\_b << endl;
          

          }

          and to my surprise, the output:

          my1: 3 2
          my2: 3 2
          my3: 0 0
          my4: 2 3

          Well I'm really upset about. What do you think?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #6

          CPallini wrote:

          What do you think?

          Well, I immediately quibbled your tests, so I did some of my own...

          class MyClass
          {
          public:
          int _a, _b;
          MyClass()
          {
          TRACE0("Blank Constructor\n");
          }
          ~MyClass()
          {
          TRACE0("Destructor\n");
          }
          MyClass(int a, int b):_a(a), _b(b)
          {
          TRACE2("Parameter Constructor(%i,%i)\n", a,b);
          }
          // copy constructor and assignment operator arbitrarly messed up.
          MyClass(const MyClass & proto)
          {
          _a = 0;
          _b = 0;
          TRACE0("Copy Constructor\n");
          }
          MyClass & operator = (const MyClass & proto)
          {
          _a = proto._b;
          _b = proto._a;
          TRACE0("Copying\n");
          return *this;
          }
          };

          void main()
          {
          MyClass my1(3,2);
          MyClass my2=MyClass(30,20);
          }

          And I got:

          Parameter Constructor(3,2)
          Parameter Constructor(30,20)

          which came as a suprise. So, the second case is being "Optimised" - even with no compiler optimisation. So C++ is treating the two cases as grammatically equivalent. This is why I help here - I learn something by code-spelunking too! Iain.

          C 1 Reply Last reply
          0
          • C Cedric Moonen

            No, really, it's true...

            Cédric Moonen Software developer
            Charting control [v1.2]

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #7

            Cedric Moonen wrote:

            I don't need any assistance

            Sure? :laugh:

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            [my articles]

            1 Reply Last reply
            0
            • C Cedric Moonen

              It's because the compiler will treat this:

              CMyCLass my1 = my2;

              As this:

              CMyClass my1(my2);

              If you would have done that operation on two lines, then the assignment operator would have been called.

              Cédric Moonen Software developer
              Charting control [v1.2]

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #8

              Nope, the compiler treats

              MyClass my2 = MyClass(3,2);

              as it was

              MyClass my2(3,2);

              (I've messed up both copy constructor and assignment operator to detect it) and IMHO skipping the temporary object construction and the assignment operator is really weird, but I don't know what the C++ standard requires. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              [my articles]

              1 Reply Last reply
              0
              • C Cedric Moonen

                No, really, it's true...

                Cédric Moonen Software developer
                Charting control [v1.2]

                I Offline
                I Offline
                Iain Clarke Warrior Programmer
                wrote on last edited by
                #9

                I knew *someone* would bite - but I assumed it would be Signor Pallini. And he's the one who gave a sensible reply! Iain.

                C C 2 Replies Last reply
                0
                • I Iain Clarke Warrior Programmer

                  CPallini wrote:

                  What do you think?

                  Well, I immediately quibbled your tests, so I did some of my own...

                  class MyClass
                  {
                  public:
                  int _a, _b;
                  MyClass()
                  {
                  TRACE0("Blank Constructor\n");
                  }
                  ~MyClass()
                  {
                  TRACE0("Destructor\n");
                  }
                  MyClass(int a, int b):_a(a), _b(b)
                  {
                  TRACE2("Parameter Constructor(%i,%i)\n", a,b);
                  }
                  // copy constructor and assignment operator arbitrarly messed up.
                  MyClass(const MyClass & proto)
                  {
                  _a = 0;
                  _b = 0;
                  TRACE0("Copy Constructor\n");
                  }
                  MyClass & operator = (const MyClass & proto)
                  {
                  _a = proto._b;
                  _b = proto._a;
                  TRACE0("Copying\n");
                  return *this;
                  }
                  };

                  void main()
                  {
                  MyClass my1(3,2);
                  MyClass my2=MyClass(30,20);
                  }

                  And I got:

                  Parameter Constructor(3,2)
                  Parameter Constructor(30,20)

                  which came as a suprise. So, the second case is being "Optimised" - even with no compiler optimisation. So C++ is treating the two cases as grammatically equivalent. This is why I help here - I learn something by code-spelunking too! Iain.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #10

                  Now you have to fix all that database and network stuff! :laugh:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  [my articles]

                  1 Reply Last reply
                  0
                  • I Iain Clarke Warrior Programmer

                    I knew *someone* would bite - but I assumed it would be Signor Pallini. And he's the one who gave a sensible reply! Iain.

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #11

                    Iain Clarke wrote:

                    I knew *someone* would bite

                    WOW I missed something, anyway usually Cedric is a very nice guy.

                    Iain Clarke wrote:

                    but I assumed it would be Signor Pallini

                    Pallini is a quite docile guy too, isn't he? BTW Signor in that context was fantastic!

                    Iain Clarke wrote:

                    And he's the one who gave a sensible reply!

                    Oh, well, hence I can assume he is. :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    [my articles]

                    1 Reply Last reply
                    0
                    • I Iain Clarke Warrior Programmer

                      I knew *someone* would bite - but I assumed it would be Signor Pallini. And he's the one who gave a sensible reply! Iain.

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #12

                      Well, it's friday :rolleyes: That explains a lot :-D

                      Cédric Moonen Software developer
                      Charting control [v1.2]

                      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