c++ OOP .. iterating over list of derived objects

This is a discussion on c++ OOP .. iterating over list of derived objects within the C++ Programming forums, part of the General Programming Boards category; If I have a base class with some pure virtual functions and 2 derived classes which have their own additional ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    c++ OOP .. iterating over list of derived objects

    If I have a base class with some pure virtual functions

    and 2 derived classes which have their own additional member functions how would I iterate over a list of these and only use the ones that are of a certain derived class?

    for example.. If i have Vehicle class , and 2 derived classes Car and Bike.

    I create a list
    Code:
    list<Vehicle*> myrides;
    now only Car has the drive() member function... what would be the cleanest way to iterate over the list and only access the drive() function for which are of type Car (ignoring the bikes which dont have drive() )..

    Thanks! ..

    ( or am I doing OOP wrong? lol)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is sort of pointless to cram everything to a list<Base*>, if you now need to figure out which is which. Keep Cars in one list and Bikes in another?
    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).

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    C++ Programming/RTTI - Wikibooks, collection of open-content textbooks

    This will get you started but a lighter method would be to have a className() method in the base class and have each derived class provide a different name:
    Base::className() returns "car"
    Derived::className() returns "toyota"
    DerivedFromDerived::className() returns "corolla"
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    In OOP it is sometimes perfectly acceptable to give a definition of a virtual method that does nothing. Whilst many of the derived classes may have some special behaviour to perform, one or some of them may not have anything they need to do at all.

    For example imagine a drawing program which can represent circles, squares, triangles, and hexagons. Now imagine that there is a "rotate" method which takes an arbitrary angle to rotate the object by, and rotates the object about its center. Assuming that there are no non-uniform scaling methods here, what action would the circle class need to take? None. Does this mean that a circle should not be derived from shape? No, it probably should still be derived from shape.

    Depending on what "Bike" really is, it may be perfectly acceptable to provide a "drive" method that similarly does nothing.
    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"

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    It is sort of pointless to cram everything to a list<Base*>,
    It depends on how many heterogenous objects you intend on cramming into the list. By nature of your statement you would need separate containers for each derived type which in itself can also become a bit cumbersome. It really depends on the requirement being fulfilled and the design of the system.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Location
    Duke
    Posts
    2
    It sounds like you need C++ code to do what the following does in lua:

    Code:
    if base.drive then base.drive() end
    Dunno how though.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Quote Originally Posted by jeffcobb View Post
    C++ Programming/RTTI - Wikibooks, collection of open-content textbooks

    This will get you started but a lighter method would be to have a className() method in the base class and have each derived class provide a different name:
    Base::className() returns "car"
    Derived::className() returns "toyota"
    DerivedFromDerived::className() returns "corolla"
    Avoid RTTI. Usually it means you have a poor design. Plus it leaves you implementation defied land if you try to get names for your types (ie classes).
    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.

  9. #9
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Sorry for raising 1 month old thread, but isn't it a textbook example of a need for the Visitor pattern?

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    It is a textbook example of bumping very old threads and an example of when I should close a thread.

    Closed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 10:34 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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