Thread: Array of objects question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Array of objects question

    Hi,
    Suppose i have a class A and which has a subclass B is it possible to create an array which elements are objects of either class A or B?
    If so, how can this be done?
    Thanks..

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    If by subclass you mean B derives from A, then yes. Your array can be an array of pointers to A.
    An array of pointers to a base class can store derived objects.

    Note that you can also assign derived objects to an array of A. It will not be flagged as an error. But on this case, since the array is not storing pointers to the base class, but the base class itself, if you assign to one of its elements a derived object, only the base part of that object will be stored. It's called slicing...
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Thanks!
    But now it seems that i have a problem with the array's elements' initialization..

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Can you show the code?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Sorry, it's quite large.
    I'm just confused about the way i should call the constructor for each element after the array declaration.
    If you could show me a small example i would really be grateful

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > But now it seems that i have a problem with the array's elements' initialization..

    Such an array would be dynamically sized with operator new or built out of existing instances. You could try to hack it I suppose, but in it's most glaring form, you end up taking the address of a temporary.

    For instance, this is very wrong:

    Let foo be a class whose constructor takes an integer, and bar a derivative class whose constructer has an additional char parameter.
    Code:
    foo *array[] = { &foo(123), &bar(234, 'a'), &bar(567, 'b'), &foo(0) };

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm just confused about the way i should call the constructor for each element after the array declaration.

    You would use new to allocate space for the objects and store the pointer in the array. You would then have to remember to delete the pointers when you are done with them. A boost::ptr_vector is probably a better solution than an array because it handles the deletes for you.

    You also have to make sure that class A has a virtual destructor.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    I understand. But the problem is that i do not know how to store the pointer in the array.
    It seems that i can't just use the assignment operator "="..

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Show the code where you attempt to use the assignment operator, along with the declaration of the array.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Ok, i tried the following but nothing worked:
    Code:
    class_a *table=new class_a[5];
    class_a *node=new class_a("Jack");
    table[0]=node;
    Code:
    vector <class_a*> vec(5); 
    class_a *node=new class_a("Jack");
    vec.push_back(node);
    I really appreciate your concern..
    Thanks.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    try this
    Code:
    class_a **table=new class_a*[5];
    class_a *node=new class_a("Jack");
    table[0]=node;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Didn't work either, i got the same errors:

    expected constructor, destructor or type conversion before '=' token
    expected ',' or ';' before '=' token

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    This Is working:
    Code:
    #include <iostream>
    class class_a
    {
    	int x;
    };
    int main()
    {
    class_a **table=new class_a*[5];
    class_a *node=new class_a();
    table[0]=node;
    }
    So do you have a class definition?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Yeap, ok what you say is true indeed. I was just trying to add the pointer to the array outside a function (outside main to be honest). Thank you..
    Could you possibly explain to me why
    Code:
    class_a **table=new class_a*[5];
    is needed? I thought that an array of pointers would be ok..

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it IS an array of pointers
    new class_a*[5]; asks to allocate memory for 5 elements of the type
    class_a*

    class_a* - IS pointer to class
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. A question about dynamically creating an array of objects
    By edd1986 in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 12:30 PM
  3. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 06:26 PM