Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Purely Virtual Functions With Varying Data Types

Reply
Thread Tools

Purely Virtual Functions With Varying Data Types

 
 
Volkan YAZICI
Guest
Posts: n/a
 
      03-17-2011
Hi,

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 use a vector<Field> to
represent a row, and Field has children like IntField, StringField,
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.


Regards.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-17-2011
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
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      03-18-2011
On 17 mar, 20:41, Volkan YAZICI <(E-Mail Removed)> 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 use a vector<Field> to
> represent a row, and Field has children like IntField, StringField,
> 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.)

[snip]

What you are trying to achieve is called a variant.
Boost provides one but you can roll your own quite easily.

Concerning the interface issue, popular choice are;
Variant<int,std::string,Foo> v;
int value;

// template member access
value = v.getData<int>();
// pseudo cast
value = variant_cast<int>(v),
// named member function (for fixed set of types)
value = getInt();

There are also solutions with proxies for data binding but it is more
exotic.

--
Michael
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-18-2011

"Volkan YAZICI" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> 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 use a vector<Field> to
> represent a row, and Field has children like IntField, StringField,
> 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.
>
>

Yes variant types are quite easy to impliment here's how to do it:

#include <iostream>
#include <string>

template<typename T1 ,typename T2>
class ItemBase{
public:
virtual ~ItemBase(){}
virtual operator T1(){return 0;}
virtual operator T2() {return 0;}
virtual ItemBase<T1,T2>& getData(){;return *this;}
};

template<typename T, typename T1, typename T2>
class DataItem: public ItemBase<T1,T2>{
T itsData;
public:
DataItem(const T& p):itsData(p){}
operator T(){return itsData;}
};

template<typename T1, typename T2>
class Field{
ItemBase<T1,T2>* itsData;
public:
Field(): itsData(0){}
~Field(){delete itsData;}
void setData(const T1& p){itsData = new DataItem<T1,T1,T2>(p);}
void setData(const T2& p){itsData = new DataItem<T2,T1,T2>(p);}

ItemBase<T1,T2>& getData(){return *itsData;}

};

int main(){

Field<int, std::string> f1, f2;
f1.setData("string");
f2.setData(5);

std::cout<< f2.getData();
std::cout<< (std::string)f1.getData(); /*Needs explicit cast to call
correct conversion operator*/
std::strings str = f1.getData(); /*Correct conversion is called without
explicit cast.*/

}


Note how you need to be carefull with conversion rules.
You need to add copy constructors etc to make it container safe. I left
alot out so that you can easily understand the code paths. Just add T3 , T4
etc to templates to get more types.

HTH.

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-18-2011

"Paul" <(E-Mail Removed)> wrote in message
newspLgp.124240$(E-Mail Removed)2...
>
> "Volkan YAZICI" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>>
>> 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 use a vector<Field> to
>> represent a row, and Field has children like IntField, StringField,
>> 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.
>>
>>

> Yes variant types are quite easy to impliment here's how to do it:
>
> #include <iostream>
> #include <string>
>
> template<typename T1 ,typename T2>
> class ItemBase{
> public:
> virtual ~ItemBase(){}
> virtual operator T1(){return 0;}
> virtual operator T2() {return 0;}


Note I would like to make the above functions pure virtual because they are
never supposed to be called, but can't seem to do it, perhaps if I spent
more time on it.



> virtual ItemBase<T1,T2>& getData(){;return *this;}
> };
>
> template<typename T, typename T1, typename T2>
> class DataItem: public ItemBase<T1,T2>{
> T itsData;
> public:
> DataItem(const T& p):itsData(p){}
> operator T(){return itsData;}
> };
>
> template<typename T1, typename T2>
> class Field{
> ItemBase<T1,T2>* itsData;
> public:
> Field(): itsData(0){}
> ~Field(){delete itsData;}
> void setData(const T1& p){itsData = new DataItem<T1,T1,T2>(p);}
> void setData(const T2& p){itsData = new DataItem<T2,T1,T2>(p);}
>
> ItemBase<T1,T2>& getData(){return *itsData;}
>
> };
>
> int main(){
>
> Field<int, std::string> f1, f2;
> f1.setData("string");
> f2.setData(5);
>
> std::cout<< f2.getData();
> std::cout<< (std::string)f1.getData(); /*Needs explicit cast to call
> correct conversion operator*/
> std::strings str = f1.getData(); /*Correct conversion is called without
> explicit cast.*/
>
> }
>
>
> Note how you need to be carefull with conversion rules.
> You need to add copy constructors etc to make it container safe. I left
> alot out so that you can easily understand the code paths. Just add T3 ,
> T4 etc to templates to get more types.
>
> HTH.
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to structure a class with a variable with varying types Angus C++ 6 08-21-2009 05:58 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Functions with varying number of arguments dima@inotech.ru C++ 12 02-16-2006 10:01 AM
Transform varying input types to XML in JAVA chetan.33@gmail.com XML 0 07-05-2005 07:03 AM
How do I pass an array of varying types to a function? Alan Silver ASP .Net 9 06-23-2005 06:41 PM



Advertisments