Array of Base(Abstract) Class Problem

This is a discussion on Array of Base(Abstract) Class Problem within the C++ Programming forums, part of the General Programming Boards category; Hello, Okay, here's the problem. I'm trying to create a linked list of abstract Object types, so I can use ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Farther than you think
    Posts
    11

    Array of Base(Abstract) Class Problem

    Hello,

    Okay, here's the problem. I'm trying to create a linked list of abstract Object types, so I can use more complex (and extending) derivations of this Object class in the linked list. Follow so far?

    The code in the game is more complex, but this code below gives the right idea (I've used an array instead of a linked list for simplicity)

    Code:
    class Base{
    
        public:
    
            virtual void doSomething(){
                std::cout << " - - - - " << std::endl;
            }
        
    };
    
    // - - - - - - - - - - - - - - - - - - - - - - - -
    
    class Extended : public Base{
    
        public: 
    
            void doSomething(){
                std::cout << "Doing something..." << std::endl;
            }
        
    };
    
    // - - - - - - - - - - - - - - - - - - - - - - - -
    
    int main(int argc, char* argv[]){
    
        Base baseObj;
        Extended extensionObj;
        
        Base objs[2];
        objs[0] = baseObj;
        objs[1] = extensionObj;
    
        objs[0].doSomething();
        objs[1].doSomething();
    
        return 0;
    
    }
    Output only gives:

    - - - -
    - - - -


    Rather than:

    - - - -
    Doing something...


    Could someone please point me in the right direction? I don't understand why the overriding method in objs[1] is not called, like it should be..Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I do not know if this is right:

    Extended *extensionObj = new Extended ;

    extensionObj->doSomething();


    EDIT:
    This works, but I have no idea if its the right thing to do

    Code:
    #include <iostream>
    
    class Base{
    
        public:
    
            virtual void doSomething(){
                std::cout << " - - - - " << std::endl;
            }
        
    };
    
    // - - - - - - - - - - - - - - - - - - - - - - - -
    
    class Extended : public Base{
    
        public: 
    
            void doSomething(){
                std::cout << "Doing something..." << std::endl;
            }
        
    };
    
    // - - - - - - - - - - - - - - - - - - - - - - - -
    
    int main(int argc, char* argv[]){
    
        Base baseObj;
        Extended *extensionObj = new Extended;
           
        Base *objs[2];
        objs[0] = &baseObj;
        objs[1] = extensionObj;
    
        objs[0]->doSomething();
        objs[1]->doSomething();
        
    
        return 0;
    }
    Last edited by h3ro; 03-13-2008 at 11:12 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Farther than you think
    Posts
    11
    Thanks very much! I've implemented those changes into my game and everything works well.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Ah yes, polymorphism only works with pointers.
    To go with your original example:
    Code:
        Base baseObj;
        Extended extensionObj;
        
        Base* pObjs[2];
        pObjs[0] = &baseObj;
        pObjs[1] = &extensionObj;
    
        pObjs[0]->doSomething();
        pObjs[1]->doSomething();
    Should also do the trick.
    Though it isn't required, I would mark every virtual function in derived classes as virtual, as well (the compiler will treat them as virtual anyway, but it's better to be explicit).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    I'm trying to create a linked list of abstract Object types, so I can use more complex (and extending) derivations of this Object class in the linked list.
    Are you trying to create a cosmic hierarchy of classes based on an Object base class? If so, note that in C++ such genericity is normally provided by templates instead.

    I've implemented those changes into my game and everything works well.
    I hope you noticed that h3ro's example fails to match new with a corresponding delete. Your code should do better. You could use std::tr1::shared_ptr with std::list or your own linked list class, or you could try Boost's ptr_list.

    Ah yes, polymorphism only works with pointers.
    ... and references, though in this case only pointers are applicable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Note that arrays of base classes are a big no-no. Arrays of pointers, yes, but never directly of the class type.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by CornedBee View Post
    Note that arrays of base classes are a big no-no. Arrays of pointers, yes, but never directly of the class type.
    ...only when trying to do polymorphism, right?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    ...only when trying to do polymorphism, right?
    Yes, but that's the point of (abstract)base classes, right?

    In fact, any class that is inherited from another class would need to be a pointer, or you'd get "slicing". [Where the stored class is chopped to the size of the base].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >Yes, but that's the point of (abstract)base classes, right?

    Yeah, I guess so.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Aramil View Post
    Array of Base ..... create a linked list of ..... in the linked list ..... an array instead ..... a linked list ..... Base objs[2];
    Sheesh, make your freakin mind up already!

    They are totaly different sutrctures with very different properties. Sometimes you can use either, but usually one is a better choice than the other. You can't always expect to use the less appropriate one and get away with it (speed-wise).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Sheesh

    The code in the game is more complex, but this code below gives the right idea (I've used an array instead of a linked list for simplicity)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. intArray class problem!
    By nag in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2003, 10:43 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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