Polymorphism question

This is a discussion on Polymorphism question within the C++ Programming forums, part of the General Programming Boards category; I'm currently working on a web application in which I have an Element class for objects on the page, and ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Polymorphism question

    I'm currently working on a web application in which I have an Element class for objects on the page, and Tag classes for the HTML tags that make up those page objects:
    Code:
    #include <string>
    using std::string;
    
    class Tag { // represents all tags
      // ...
      public:
        const virtual string write()=0; // should return the HTML to represent the tag
    }
    
    class EmptyTag : protected Tag { // for tags like <img/>, <br/>, etc.
      //...
      public:
        const string write();
    }
    
    class EnclosingTag : protected Tag { // for tags like <p>text here</p>
      //...
      public:
        const string write();
    }
    Each instance of the Element class should contain an ordered, variable sized set of Tag objects. The references I've read have generally only referred to polymorphism in the context of pointers:
    Code:
    class Base;
    class Derived : public Base;
    class OtherDerived : public Base;
    Base* polymorphicPointer;
    but have not said whether and how it can be used in other contexts. Would it be possible to just use a vector?
    Code:
    std::vector<Tag> myTags;
    or would I have to use some more complicated solution?
    If so, what would people suggest?

    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Most implementations to solve this that I have seen seem to favor a collection of pointers to objects, i.e.

    Code:
    std::vector<Tag*> myTags;
    
    myTags.push_back( new EnclosingTag );
    myTags.push_back( new EmptyTag );
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    ok, thanks for your help.
    I guess I'll try to forget my OCD desire to save every single byte of memory and just use a vector of pointers.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    References would also be legal to use that way.

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...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. A question Polymorphism
    By omeydhomey in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2003, 10:28 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