Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RageMatrix

Arrays of objects...

This topic is 6113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm pretty sure I know the answer to this, but I just wanted to make sure. Say I have a class like this: -
    
class CObject
{
  public:
     CObject(int param1, int param2);
     ~CObject();
  private:
     int num1;
     int num2;
};
  
If I wanted to make an array of these objects, say like this: -
  
CObject oCollection[MAX_OBJECTS];
  
I would have to include the constructor parameters to this instead:-
        
class CObject
{
  public:
     CObject(void);
     void Create(int param1, int param2);
     ~CObject();
  private:
     int num1;
     int num2;
};
  
The reason being that when the objects are declared in the array, they are created at this time. Therefore, you couldn't pass the the parameters to the constructor in this manner. But you could do this: -
        
for (int i=0;i<MAX_OBJECTS;i++)
{
 oCollection[i].Create(i+1, i+1);
}
  
This is right, isn't it? Collections of objects cannot have parameters passed to them via the constructor.. Although, logically, I suppose there would be no way to achieve this anyhow. Sorry if this seems like a stupid question, but its part of my New Years resolution to be a better programmer by asking about stuff I'm not sure about, or have taken as a given. At work, theres always a better solution that the one you've thought of (I learnt this last week ). I guess I'm just wondering why so many books have examples with constructors taking parameters, when this means that you could never have a large number of them present in an array when designed in this manner. Cheers, RM. Tron Software -=Kicking Butt and Writing Code=- Edited by - RageMatrix on December 27, 2001 8:10:30 PM

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by RageMatrix
This is right, isn''t it? Collections of objects cannot have parameters passed to them via the constructor


That is correct. You can fake it by making a std::vector (or really any container) of your objects and pushing-back/inserting one at a time, but that requires dynamic memory allocation, a working copy ctor in most cases and an extra copy when compared to a plain-old array.

Share this post


Link to post
Share on other sites
You can pass parameters to an array of class objects via
the constructor. I don't know the code flag for the forum,
but you should be able to read this.

  
class Sale
{
private:
float tax;
float total;
public:
Sale(float rate)
{
tax = rate;
}
Sale(float rate, float cost)
{
tax = rate;
calc(cost);
}
void calc(float cost)
{
total = cost + (cost * tax);
}
float getTotal()
{
return total;
}
};

Now we create an array of Sale objects and we first use
the first constructor and then we use the second.

Also notice how the constructor can also call member functions.

  
Sale items[2] = {.06, Sale(.09, 13.45)};


The example was straight out my book.
Some notes:
If there are fewer initializer calls in the list then objects
in the array, the default constructor will be called for the
remaining objects.(I left out the default constructor in this.)

If there is no default constructor you must furnish an
initializer for each object in the array.

If a constructor requires more than one argument, the
initializer takes the form of a constructor function call
(as seen in the example).

Create.
Liv Tyler makes a really great elf.

Edited by - RolandofGilead on December 27, 2001 9:42:24 PM

Edited by - RolandofGilead on December 27, 2001 9:47:42 PM

Edited by - RolandofGilead on December 27, 2001 9:49:24 PM

Edited by - RolandofGilead on December 27, 2001 12:18:29 AM

Share this post


Link to post
Share on other sites
RolandofGilead, The code flag in the forum is [ source ] and [ /source ], without the spaces

No, HTML is not an OO language.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!