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. Syntax problem

Syntax problem

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
8 Posts 6 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.
  • J Offline
    J Offline
    Jay03
    wrote on last edited by
    #1

    This syntax with all those brackets is scaring me.... Can someone please tell me what is going on? for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; } Thank You! :)

    D K V C Z 5 Replies Last reply
    0
    • J Jay03

      This syntax with all those brackets is scaring me.... Can someone please tell me what is going on? for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; } Thank You! :)

      K Offline
      K Offline
      Kharfax
      wrote on last edited by
      #2

      Is casting whatever there is inside ms_instances array to a Ball object pointer, and then is refering to that object that is beign pointed, you can resume it like this: Ball b; b.m_stateTimeInterval = timeInterbal; Obviously dont replace the code, is just for you to see the same in a more simpler way Hope it helps

      1 Reply Last reply
      0
      • J Jay03

        This syntax with all those brackets is scaring me.... Can someone please tell me what is going on? for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; } Thank You! :)

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        It could also be written as:

        for (...)
        {
        Ball *pBall = ms_instances[j];
        ( *pBall ).m_stateTimeInterval = timeInterval;
        // or
        pBall->m_stateTimeInterval = timeInterval;
        }


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        1 Reply Last reply
        0
        • J Jay03

          This syntax with all those brackets is scaring me.... Can someone please tell me what is going on? for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; } Thank You! :)

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          I would prefer this:

          for ( unsigned int j = 0; j < ms_numInstances; j++ )
          {
              Ball * const ball = ms_instances[j];
              ball->m_stateTimeInterval = timeInterval;
          }
          

          Now it seems much clearer, I think. The ms_instances array contains pointers to Ball objects. This loop initializes the m_stateTimeInterval field of all of Ball objects.

          J 1 Reply Last reply
          0
          • J Jay03

            This syntax with all those brackets is scaring me.... Can someone please tell me what is going on? for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; } Thank You! :)

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            ms_instances is apparently an array of pointers to instances of some kind of object. this code gets element 'j' (a pointer) out of the array, then casts that pointer to a pointer-to-a-Ball: "compiler, treat this pointer as if it was a pointer to a Ball, instead of a pointer to whatever it is actually pointing at. then it dereferences the pointer and sets the m_state* member variable of what we all hope is an actual Ball object.

            image processing | blogging

            1 Reply Last reply
            0
            • J Jay03

              This syntax with all those brackets is scaring me.... Can someone please tell me what is going on? for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; } Thank You! :)

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              It is nothing more than the equivalent of doing the following:

              for (unsigned int j = 0; j < ms_numInstances; j++)
              {
              	Ball* pBall = (Ball*)ms_instances[j];
              	pBall->m_stateTimeInterval = timeInterval;
              }
              

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              1 Reply Last reply
              0
              • V Viorel

                I would prefer this:

                for ( unsigned int j = 0; j < ms_numInstances; j++ )
                {
                    Ball * const ball = ms_instances[j];
                    ball->m_stateTimeInterval = timeInterval;
                }
                

                Now it seems much clearer, I think. The ms_instances array contains pointers to Ball objects. This loop initializes the m_stateTimeInterval field of all of Ball objects.

                J Offline
                J Offline
                Jay03
                wrote on last edited by
                #7

                Viorel. wrote:

                The ms_instances array contains pointers to Ball objects

                What happens when j = 0; Ball * const ball = ms_instance[0]; Does this mean it has no pointers because ms_instance[0] ? Do you need atleast j = 1 to create a pointer ........ like ms_instance[1] means 1 pointer .... ms_instance[2] means array of two pointers......... so what does 0 mean? ball->m_stateTimeInterval = timeInterval; -------------------------------------------------------------------------------------------- So for each j value, there will be an array of pointers created?

                Z 1 Reply Last reply
                0
                • J Jay03

                  Viorel. wrote:

                  The ms_instances array contains pointers to Ball objects

                  What happens when j = 0; Ball * const ball = ms_instance[0]; Does this mean it has no pointers because ms_instance[0] ? Do you need atleast j = 1 to create a pointer ........ like ms_instance[1] means 1 pointer .... ms_instance[2] means array of two pointers......... so what does 0 mean? ball->m_stateTimeInterval = timeInterval; -------------------------------------------------------------------------------------------- So for each j value, there will be an array of pointers created?

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #8

                  Jay03 wrote:

                  Ball * const ball = ms_instance[0];

                  Arrays in C/C++ are 0-based. This line will access the first element in the ms_instance array and cast it to a pointer-to-a-Ball.

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                  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