A question Polymorphism

This is a discussion on A question Polymorphism within the C++ Programming forums, part of the General Programming Boards category; hey everyone, im doing this program on polymorphism and inheritance and what our teachjer wants us to do is to ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    A question Polymorphism

    hey everyone, im doing this program on polymorphism and inheritance and what our teachjer wants us to do is to insert things from the derived class into a base class array which i was able to do. but she also wants us to change the information in teh array and i dont know how to do that. what ive tried to do was somthing like this

    TimePiece *Array[10];

    price = Array[0].getPrice();

    and then i get to change the price and then put it back in tht position, but the thing is you cant do that with *Array[10], you can only do it with a non-pointer array. so please help. Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Code:
    MyObject obj;
    MyObject *p;
    
    p = &obj;
    
    obj.method();
    p->method();
    .....
    MyObject objArray[10];
    MyObject *pObjArray[10];
    ...
    objArray[5].method();
    pObjArray[5]->method();
    Clear things up any?

    gg

  3. #3
    Just
    Guest
    This question verges on something that's been bothering me for a while now. I was under the impression that polymorphism and arrays in C++ don't mix. At least that's what Meyers keeps saying in More Effective C++.

    The problem, as I understand it, is that the pointer arithmetic needed for arrays doesn't work properly, because a pointer in an array cannot be increased by the right amount. Pointers are only incremented by the size of an object belonging to the base class, not the derived class. So doing something like myArray[2] won't get you to the start of the 3rd object, because the [] operator will only go by the size of the base class objects. (I'm sure someone else can explain that better).

    But Meyers also says that you're unlikely to encounter this problem if your base class is abstract. "Unlikely" is a bit worrying, though - can you still encounter this problem anyway? Anyone?

    Thanks,

    Just

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    When he says unlikely he means impossible -- you can't instantiate abstract base classes in the first place, so you can't make an array of them. His point is that you can't make an array of objects who share the same base because they can be different sizes (non-abstract) -- that's why you'd make an array of POINTERS to the base type. Then you just make the elements point to the proper objects, usually with dynamic memory allocation.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    using arrays as codeplug showed you is fine. What meyers talks about is the problem of sending into a function an array of derived when an array of base is expected. something like this...
    Code:
    class A { // virtual funcs 
                };
    
    class B : public A {// even more virt funcs and data
                                };
    
    ostream& printAs( ostream & , A [] );
    .
    .
    .
    // in main
    A Aarray[] = { //init}
    B Barray[] = { //init}
    printAs(cout,Aarray); // works fine
    printAs(cout,Barray); // compiles becaues a derived is a base but BANG!!
    Last edited by Stoned_Coder; 03-09-2003 at 08:33 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Yes, and the only time pointer values change is with multiple inheritance, observe:
    Code:
    class Stoned_Coder
    {
    public:
        void foo() {cout << "Free it!" << endl;}
    };
    
    class CodePlug
    {
    public:
        void foo() {cout << "Yes, free it!" << endl;}
    };
    
    class PlugStoned: public Stoned_Coder, public CodePlug 
    {
    };
    
    ...
    
    PlugStoned ps;
    
    Stoned_Coder *sc = static_cast<Stoned_Coder*>(&ps);
    CodePlug *cp = static_cast<CodePlug*>(&ps);
    
    cout << "Stoned_Coder = " << sc << endl;
    cout << "CodePlug = " << cp << endl;
    This will output (on my machine):
    Stoned_Coder = 0012FF6C
    CodePlug = 0012FF70
    Looks like my pointer is bigger

    This is never a problem with arrays since (a) all pointers are the same size, (b) casting takes care of all the magic.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. Polymorphism newbie question
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:38 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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