Thread: Using Subclasses

  1. #1
    Registered User jmpeer's Avatar
    Join Date
    Jan 2010
    Posts
    5

    Using Subclasses

    Hey, quick question.
    I want to make a Class and a few SubClasses.
    Then I want to make a dynamic Container for these SubClasses.
    All these SubClasses are going to have a function that the Container is going to call.
    But I expect to add more SubClasses later, so I don't want to write anything specific to those classes in Container.

    What is the best way of handling this situation?
    I know malloc and all that, so you don't need to treat any suggestions as too, too simple.
    I just want Container to have some kind of list of these SubClasses and to be able to call a function
    in this list of varying classes.
    Last edited by jmpeer; 05-03-2010 at 09:33 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As for dynamic list, why not use, oh I don't know, "list"?

    To use virtual methods or similar, you would need to store pointers-to-ParentClass, not ParentClass objects themselves.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jmpeer View Post
    I know malloc and all that, so you don't need to treat any suggestions as too, too simple.
    malloc()??!! Noooooo, this is C++. You need to use new, not malloc(), especially with classes.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors and subclasses
    By Siphon in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2007, 10:51 AM
  2. Classes, Subclasses & Dynamic Memory
    By CaeZaR in forum C++ Programming
    Replies: 6
    Last Post: 02-06-2006, 06:07 PM
  3. virtual functions and poniters to subclasses
    By Asagohan in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2005, 09:52 AM
  4. Replies: 3
    Last Post: 07-23-2005, 08:00 AM
  5. Subclasses
    By Korn1699 in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2002, 01:19 PM