Re: Purely Virtual Functions With Varying Data Types
On 3/17/2011 3:41 PM, Volkan YAZICI wrote:
I'd like to implement a simple database, that is, a collection of
tables, where the tables are just a vector of rows, and rows are just
a vector of fields with varying data types.
I am not sure that I'd do it that way. Nothing simple about trying to
squeeze the model of a generic record with unknown number of fields of
unknown types into a standard container. The standard containers aren't
suited for that, IMO.
> I use a vector<Field> to
represent a row, and Field has children like IntField, StringField,
"Children"? You mean, derived classes?
etc. Field class would have a getData() method, which is obviously
expected to be defined as a purely virtual function in Field. But the
problem is, what would be the function footprint of such a purely
virtual function? That is, for IntField, it will be "const int&
getData() const"; for StringField, it will be "const string& getData()
const", etc. Can anybody help me on this, please? How should I
approach the problem? Do you recommend another solution? (BTW, Field
is also expected to have relevant ctor and comparison methods as well.
FYI.)
OTOH, one can argue that, why not using sth like Field<int>(),
Field<string>(), etc.? In such a case, I won't be able to define a
vector of different data types. That is, to denote a row, I'll use
vector<Field<int>>, and I'll only be able to store "int"s, no other
data types will be permitted.
AIUI, to solve this using a statically typed language like C++ one would
define 'Field' to have methods to "interpret" it depending on its
"type", something like
String asString() const;
int asInt() const;
and so on. They can be virtual or they can be non-virtual and forward
the actual call to some virtual function *after* checking the "runtime
type" of the Field.
You will likely rely heavily on casts in the implementation of those
methods.
Also, check your favorite search engine. This *undoubtedly* has been
done and probably published before.
V
--
I do not respond to top-posted replies, please don't ask