Creating Objects in Loops

This is a discussion on Creating Objects in Loops within the C++ Programming forums, part of the General Programming Boards category; Here's a concept that's been bugging me for a while. Let's imagine I have a program that goes through a ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Unhappy Creating Objects in Loops

    Here's a concept that's been bugging me for a while.

    Let's imagine I have a program that goes through a for loop ten times. On each pass, it creates a new object. My question is, after the loop has executed all ten times, how would I reference one of the objects that was just created?

    Thanks!
    ~Matt

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by MattMik View Post
    Here's a concept that's been bugging me for a while.

    Let's imagine I have a program that goes through a for loop ten times. On each pass, it creates a new object. My question is, after the loop has executed all ten times, how would I reference one of the objects that was just created?

    Thanks!
    ~Matt
    Through the pointer you got when you called 'new', which you should have stashed away somewhere...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But that's the rub -- you need to have put it somewhere, like a pre-existing array of pointers or so, or a vector of objects or similar.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I'd recommend a vector of the objects. It'd make it far simpler than new-ing the lot.
    Code:
    #include <vector>
    #include "myType.h"
    
    int main() {
      std::vector<myType> vmT;
    
      for ( int i=0; i<10; i++ ) {
        vmT.push_back( myType( i ) ); // new object with int as constructor
      }
    
      // ...
      
      vmT.at(1).square();
    
      return 0;
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If, on the other hand, you mean something like
    Code:
    for (int i = 0; i < 10; ++i) {
      object o;
    }
    then you should realize that the object is destroyed and recreated every time the loop is restarted, and destroyed when you finally leave it, so there is no object after the loop.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to rotate child objects in 3D
    By Arianiv in forum Game Programming
    Replies: 11
    Last Post: 04-03-2008, 05:09 AM
  2. Complicated member objects that depend on `this`
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 08:48 PM
  3. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  4. creating objects in c++
    By sachitha in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 12:19 PM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 01:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21