Thread: Array of Base(Abstract) Class Problem

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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,318
    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, 07:18 AM