Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: C++ how to increment objects name in a loop ?

  1. #1
    Senior Member
    Join Date
    Dec 2018
    Posts
    130

    C++ how to increment objects name in a loop ?

    Hello,

    I am not a C++ expert... and I don't know how to compact this kind of code in a loop :
    Code:
      }
      pot0.update(output[0]);
      pot1.update(output[1]);
      pot2.update(output[2]);
      pot3.update(output[3]);
      pot4.update(output[4]);
      pot5.update(output[5]);
      pot6.update(output[6]);
      pot7.update(output[7]);
      pot8.update(output[8]);
      pot9.update(output[9]);
      pot10.update(output[10]);
      pot11.update(output[11]);
      pot12.update(output[12]);
      pot13.update(output[13]);
      pot14.update(output[14]);
      pot15.update(output[15]);
    }
    It's easy to increment output[i] using " for (int i = 0;i<16;i++) " , but do you increment pot"i".udpdate() objects ?
    I guess you make an array of objects and then use pointers, but I am lost.

    Emmanuel

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,443
    This should work
    Code:
    pot pots[16];
    
    void update()
    {
        for (int i = 0; i < 16; i++)
        {
            pots[i].update(output[i]);
        }
    }

  3. #3
    Senior Member
    Join Date
    Dec 2018
    Posts
    130
    Thank you !
    I follow your idea using pointers and it works .

  4. #4
    Senior Member
    Join Date
    Jul 2020
    Posts
    966
    No pointers in that code, just an array of objects. If pointers were involved you'd replace '.' by '->'
    (or is that what you've done?)

  5. #5
    Senior Member
    Join Date
    Dec 2018
    Posts
    130
    Yes I have replaced '.' by '->'.
    I will try arrays of objets, it is new to me.
    Thaks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •