Array of objects question

This is a discussion on Array of objects question within the C++ Programming forums, part of the General Programming Boards category; Hi, Suppose i have a class A and which has a subclass B is it possible to create an array ...

  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
    Portugal
    Posts
    7,467
    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...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,467
    Can you show the code?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    > 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,318
    >> 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,318
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    try this
    Code:
    class_a **table=new class_a*[5];
    class_a *node=new class_a("Jack");
    table[0]=node;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 11:30 AM
  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

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