H
hall
I have a problem with my design of a templatized class. I'm trying to
figure out how to load and save the data inside it, but can't. My class
looks like this
------------------------------------
template <class tType> class Foo {
private:
tType data;
public:
load(InFile &in){
// Can't figure out what to write here
}
};
------------------------------------
my question is how to write the load function.
InFile is more or less a wrapper around an instream object that adds
some functionality missing in the basic classes. The most important
feature (in this context) is that binary and textfiles are treated
exactly the same by the user, eg load a float from an open file (inf is
the InFile object) is done by
float f;
inf >> f; // for text file
inf >> f; // for binary file
This makes file I/O very simple in my program where i need to read and
write to files in both binary and text format.
InFile has the operator >> overloaded for the basic types (integers,
floats, bool, char) as members of InFile, and for a few additional
classes. For these it would be nice to write the load function as
load(InFile & inf){ inf >> data; }
The problem is that the user of the class Foo could put any type of
object and as tType could be a type for which there is no overloading of
the >> operator, things would go wrong. In fact, i'm not sure that Foo
would compile as the compiler cannot know if tType is always a type for
which (inf >> tType) is allowed.
I'd appreachiate any kind of help or hints on how to solve this. If you
need more info in the InFile class, I can post a simple version of it.
regards
hall
figure out how to load and save the data inside it, but can't. My class
looks like this
------------------------------------
template <class tType> class Foo {
private:
tType data;
public:
load(InFile &in){
// Can't figure out what to write here
}
};
------------------------------------
my question is how to write the load function.
InFile is more or less a wrapper around an instream object that adds
some functionality missing in the basic classes. The most important
feature (in this context) is that binary and textfiles are treated
exactly the same by the user, eg load a float from an open file (inf is
the InFile object) is done by
float f;
inf >> f; // for text file
inf >> f; // for binary file
This makes file I/O very simple in my program where i need to read and
write to files in both binary and text format.
InFile has the operator >> overloaded for the basic types (integers,
floats, bool, char) as members of InFile, and for a few additional
classes. For these it would be nice to write the load function as
load(InFile & inf){ inf >> data; }
The problem is that the user of the class Foo could put any type of
object and as tType could be a type for which there is no overloading of
the >> operator, things would go wrong. In fact, i'm not sure that Foo
would compile as the compiler cannot know if tType is always a type for
which (inf >> tType) is allowed.
I'd appreachiate any kind of help or hints on how to solve this. If you
need more info in the InFile class, I can post a simple version of it.
regards
hall