I'm implementing a toy SQL database. In my code I'd like to have an abstract class Field
and its two derived classes IntField
and StringField
. Also I'd like to have a Tuple
class which represents a tuple of fields. Of course, I need to have some container of fields in my Tuple
class and this is what my code looks like at the moment:
// This is an abstract class, but the methods here are irrelevant
class Field {
virtual Type GetType() = 0;
}
// This is a concrete implementation of Field
class IntField : Field {
Type GetType() override { <some code> }
}
// This is another concrete impl of Field
class StringField: Field {
Type GetType() override { <some code> }
}
// This class represents a tuple of Fields
class Tuple {
private:
std::vector<std::shared_ptr<Field>> fields;
public:
// Don't bother in this example that i can be out of range
std::shared_ptr<Field> GetField(int i) { return fields[i]; }
void SetField(int i, std::shared_ptr<Field> f) {
fields[i] = f;
}
}
I understand I cannot have a std::vector<Field>
as Field
class cannot be instantiated, thus I need to use smart pointers. What bothers me is the getting and setting of the fields are handled. Can this be done in some better way?