Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Question for a senior C++ developer. Dare you?

Question for a senior C++ developer. Dare you?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 4 Posters 2.4k Views 3 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1

    What must be in SR.h to achieve desired printed values?

    #include <stdio.h>
    #include "SR.h"
    
    int main()
    {
        int j = 1;
        int a[] = {2, 3};
        {
            SR x(j), y(a[0]), z(a[1]);
    
            j = a[0];
            a[0] = a[1];
            a[1] = j;
    
            printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
            //        j = 2, a = {3, 2} - we want that the printed be this
        }
        printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
        //        j = 1, a = {2, 3} -  - we want that the printed be this
    }
    ```
    raven-worxR 1 Reply Last reply
    0
    • K Kofr

      What must be in SR.h to achieve desired printed values?

      #include <stdio.h>
      #include "SR.h"
      
      int main()
      {
          int j = 1;
          int a[] = {2, 3};
          {
              SR x(j), y(a[0]), z(a[1]);
      
              j = a[0];
              a[0] = a[1];
              a[1] = j;
      
              printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
              //        j = 2, a = {3, 2} - we want that the printed be this
          }
          printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
          //        j = 1, a = {2, 3} -  - we want that the printed be this
      }
      ```
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Kofr
      so you want instead of printing

      j = 2, a = {3, 2}
      j = 2, a = {3, 2}
      

      it should print

      j = 2, a = {3, 2}
      j = 1, a = {2, 3}
      

      right???

      If so you need to get around the scoping and create new "inner" variables" and create a copy of the "outer" variable values.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @raven-worx, looks like SR is just useless since it's not used anywhere.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          To add to @raven-worx, looks like SR is just useless since it's not used anywhere.

          K Offline
          K Offline
          Kofr
          wrote on last edited by
          #4

          @SGaist solution is made by SR.

          1 Reply Last reply
          0
          • kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            Here:

            template<class T>
            class SR
            {
            public:
                SR(T & v)
                    : ref(v), val(v)
                {
                }
            
                ~SR()
                {
                    ref = val;
                }
            
            private:
                T & ref, val;
            }
            

            But what would be the purpose of this, beside being an example of bad programming?

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            2

            • Login

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