Thread: Array of Objects <NEWBIE>

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Angry Array of Objects <NEWBIE>

    Just woundering if there is away to make an array of objects, yet at the same time use the constructors within' the class.

    //This is DOES NOT WORK. But it's what i want to do.
    stock s[2];
    s[0]("Toys R Us",50,2.99);
    s[1]("iCafe Canada",100, 6.99);

    I dont know if this is even possible.

    Any help would be appretiated.

    Ulysses

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It's not possible in standard C++ to pass arguments for arguments to the constructor of different objects in an array. I believe gcc has an extension, however which enables it, but it is still not legal C++. You can get the same result (with the price of calling two constructors for each object), like the following.

    Code:
    stock s[2]; // calls stock's default constructor twice
    s[0] = stock("Toys R Us",50,2.99); // construct and assign a new stock/
    s[1] = stock("iCafe Canada",100, 6.99);
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Nope, you can't. I just checked. You can, however, make an array of pointers. Something like this:
    Code:
    stock *s[2];
    s[0] = new stock("Toys R Us",50,2.99);
    s[1] = new stock("iCafe Canada",100,6.99);
    s[2] = new stock("Disney",150,8.99);
    
    cout << s[0]->GetStockName() << ": " << s[0]->GetStock() << endl;
    ...
    That works. Hope that helps!

    Brendan
    Draco dormiens nunquam titillandus

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    So make the default constructor do nothing...
    Code:
    ...
    stock() {}
    ...

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Oh wait yes you can, I did something way wrong lol. Yes just do that. Lol sorry.

    Brendan
    Draco dormiens nunquam titillandus

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    But then you get the responsibility of managing dynamic memory. I would just use a vector.

    Code:
    std::vector<stock> stocks;
    stocks.push_back(stock("arg1", arg2, etc));
    stocks.push_back(stock(...));
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Thanx

    Thanx everyone, i used SilentStrike example and it seems to work.

    My only issue now is, i have to pass that array to functions, and i'm going to assume that won't be to hard.

    But am i losing alot of resources, or is it a poor way to code, if i make an array vs, two seperate objects?

    originaly i had

    stock s1("Blah", 2,2.33);
    stock s2("Blah2", 5,55.55);

    But i thought it would be easier and more effencent to pass an array of objects to a FUNCTION rather then passing 2 objects. Is my logic wrong?

    Ulysses

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Harryp, you write past the end of the array in your code.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Originally posted by SilentStrike
    Code:
    std::vector<stock> stocks;
    stocks.push_back(stock("arg1", arg2, etc));
    stocks.push_back(stock(...));
    I didn't even think about doing that... good idea

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Did I? Good Heavens!!! Well, just goes to show how stupid I can really be. As a general rule, I'm always trying to help, but take caution with the advice I give as I'm an idiot lol and it could very well have something wrong with it.

    Brendan
    Draco dormiens nunquam titillandus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 09:24 AM