-
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.
-
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 );
-
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.
-
References would also be legal to use that way.