1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ - Objects pointers array

  1. Jan 12, 2012 #1
    1. The problem statement, all variables and given/known data
    suppose i have class A.
    now i have 2 derative classes: A_1, A_2.

    i would like to create a 4'th class: Array_Class.
    Array_Class will have 2 pointers array (one for each class - A_1, A_2).
    i want to be able to get an object pointer from class A_1 or A_2, and place it in the right array.
    how should i do it?
    another importent issue: i am not allowed to use virtual functions.

    3. The attempt at a solution
    I thought of saving in each class (A_1, A_2) a type variable, and in Array_Class to do switch or somthing to know in which array save the pointer.
    I know this is no good proggraming, but is there bettwer way to do it ? (again - without using virtual functions).

    hope i was clear...
    Thanks!
     
  2. jcsd
  3. Jan 12, 2012 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Wait, so what you want is something like this?
    Code (Text):

    class {
      A1** a1s;
      A2** a2s;
     
      void addPointer(A* a) {
        // add a to a1s or a2s depending on its type
      }
     
    That is, as you say, terrible programming and any teacher giving you such an assignment should be locked away!

    You should make a single array of pointers to A's, and use polymorphism, i.e. so you can call
    Code (Text):

    A* a = arrayOfAs[i];
    a->method();
     
    without knowing if a is of subtype A1 or A2.
    As soon as your find yourself writing checks like "if(a is of type A1) { doSomething(); } else if(a is of type A2) { doSomethingElse(); }" you should seriously reconsider your design or else you may just as well switch back to C and get rid of those pesky object-thingies.

    That said, if you keep insisting on abusing the features of C++, the "a is of type A1" line in my pseudocode above can be implemented with dynamic_cast:
    Code (Text):

    bool isA1 = !(dynamic_cast<A1*>(a) == null);
     
     
  4. Jan 12, 2012 #3
    Yes, this is the excercise we got.
    and we got the instruction - not use virtual function, not use casting...
    so i think even the "terrible" "a is of type A1" i can't write...
     
  5. Jan 12, 2012 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Use overloading.
    Code (Text):

    class Array_Class {
       add_pointer (A_1 * ptr);
       add_pointer (A_2 * ptr);
       ...
    };
     
  6. Jan 12, 2012 #5
    Thanks! i think overloading is the answer!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ - Objects pointers array
  1. Array C++ (Replies: 2)

  2. C - array (Replies: 2)

Loading...