Archived

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

simple question about class constructors...

This topic is 5114 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

Hi, I have a bunch of classes in an array and I need to call the constructor for each class individually what's the best way to do that? And another question let's say I have two classes..
 class POO { ... };

 class FOO
 {
  public:
  FOO();
  private:
   POO p;
 };
   
How can I call p's constructor from FOO? Thanks. [edited by - NullPointer33 on December 13, 2003 3:59:44 AM]

Share this post


Link to post
Share on other sites
The default POO constructor (the one with no arguments) is invoked during the FOO constructor by default. To use a different POO constructor, use an initializer list.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Ok, thanks that clears it up a little bit I''ll try it later can''t right now. But what about my first question? How can I call the constructor for each class individually in an array for example let''s say POO''s constructor takes a value and I have something like


POO poo[3];


How can I call the constructor for poo[0], poo[1], and poo[2] if they have different values?

Share this post


Link to post
Share on other sites
You can''t. You can only invoke the default constructor.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Dynamically, it can't be done; for a static array, try something like:

int arr[ 6 ] = { 0, 1, 2, 3, 4, 5 };
std::vector<int> cows[ 3 ] = { std::vector<int>(5),
std::vector<int>(arr, arr + 6),
std::vector<int>() };


edit: < stuff

[edited by - SiCrane on December 13, 2003 5:43:53 PM]

Share this post


Link to post
Share on other sites