Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:15:29+00:00 2026-06-11T00:15:29+00:00

How can one iterate through an array to manipulate or access different objects and

  • 0

How can one iterate through an array to manipulate or access different objects and their member functions? I have 10 objects. Right now I have the same code accessing each object member function and manipulating object data basically copied and pasted for each object. I’m just wondering if there is a way to use a loop to write that code one time and have it loop through all 10 objects.

Instead of doing so manually like below:

Color red.set();
Color green.set();
Color blue.set();
Color yellow.set();
Color purple.set();
      ...

Is there a way to do this with a loop, such as the following:

colors[5] = {"red", "green", "blue", "yellow", "purple", ...};

for(int i = 0; i < 10; i++){
    Color colors[i].set();
}

I know that for PHP to do something similar would be this:

$colors = array("red", "green", "blue", "yellow", "purple" ...);

for($i = 0; $i < 10; $i++){
    ${$colors[$i]} = $colors[$i];
    // $red = "red";
}

Is it possible to do this for C++?

Below is another example as to why I am asking this and what I’m getting at: Instead of:

if(grid[row][col].ship == "red")
{
    red.setShipDamage();

    if(red.getShipSunk() == true)
        red.destroy();
}
else if(grid[row][col].ship == "green")
{
    green.setShipDamage();

    if(green.getShipSunk() == true)
        green.destroy();
}
else if( ... )

To do all of this once in a loop:

for(int i = 0; i < 10; i++)
{
    if(grid[row][col].ship == colors[i])
    {
        **colors[i]**.setShipDamage();

        if(**colors[i]**.getShipSunk() == true)
            **colors[i]**.destroy();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:15:31+00:00Added an answer on June 11, 2026 at 12:15 am

    Your question is somewhat confusing. You need to provide what the Color class does. Is this what you want?

    Color colors[5];
    char *color_txt[5] = {"red", "green", "blue", "yellow", "purple"};
    
    for (int i = 0; i < 5; i++){
        colors[i].set(color_txt[i]);
    }
    

    Based on your edited question, you can create an array of objects as I described:

    Color colors[10];
    

    Assuming each object has a default constructor. Then you can access each object through an index in the array. So your example works as expected:

    for(int i = 0; i < 10; i++)
    {
        if(grid[row][col].ship == colors[i])
        {
            colors[i].setShipDamage();
    
            if(colors[i].getShipSunk() == true)
                colors[i].destroy();
        }
    }
    

    Also, your Color class should have on overriden equality operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can one transaction update two different TVar s in an atomic way? i.e. can
I have the following json array of objects in my code var groups =
This is probably a simple question, but how do you iterate through an array,
Assume that I have some array of data (a vector to be specific). Can
For some reason when I iterate through an array using foreach loop a condition
In one of my Java 6 projects I have an array of LinkedHashMap instances
Problem: I have two arrays that can possibly be different lengths. I need to
Most examples of inplace editing are one-liners that iterate through a file or files,
So, in PHPDoc one can specify @var above the member variable declaration to hint
Stumped by this one: I have a function that returns an array of folders

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.