databaseInstance
would need to be of some type that provides operator>>
and operator<<
functions that write to the database in the way that you want, thus providing a stream-like interface. These functions will be defined somewhat like so:
database_type& operator<<(database_type& databaseInstance, MyClass& c)
{
// Write to database here
return databaseInstance;
}
database_type& operator>>(database_type& databaseInstance, MyClass& c)
{
// Read from database here
return databaseInstance;
}
This makes use of operator overloading. There's not much more to say than that and it's a pretty odd thing to do with a database as a database is not very much like a stream.
You probably want to reverse the usage of the operators for the sake of consistency with the C++ standard library:
MyClass c;
databaseInstance << c;
databaseInstance >> c;
However, it's uncertain exactly what the final line would read into your MyClass
object. The second line only really makes sense if this databaseInstance
has been configured to insert into a particular table.