472,958 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

How do I load data to a templatized class


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

--
( - Remove capital X from email to reply - )

Jul 19 '05 #1
3 2920
"hall" <Xc***********@yahoo.se> wrote in message
news:3F**************@yahoo.se...
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.


You are correct that it will fail if the user puts in a type which does not
have your overloaded operator>>. However, the compiler will not prevent you
from defining a template that makes this assumption. Consider std::vector,
for example. Vector only works for types that are copy constructible, since
it may need to copy its contents when the size increases. However, the fact
that users can define a class

class NotCopyConstructible {
private: NotCopyConstructible (NotCopyConstructible const&) {}
};

that is not copy constructible does not prevent vectors from being used for
classes that do indeed meet the requirement.

Probably a good solution to this problem would be to provide a template that
will make it easier for users to define operator>> for their classes, if
this is possible. In the general case where an object is "deep" this may be
quite difficult, but if the class has no pointers or references it is very
easy.

HTH,
Kevin.
Jul 19 '05 #2


On 10/28/2003 7:11 PM Kevin Saff spoke thusly
"hall" <Xc***********@yahoo.se> wrote in message
news:3F**************@yahoo.se...
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.

You are correct that it will fail if the user puts in a type which does not
have your overloaded operator>>. However, the compiler will not prevent you
from defining a template that makes this assumption. Consider std::vector,
for example. Vector only works for types that are copy constructible, since
it may need to copy its contents when the size increases. However, the fact
that users can define a class

class NotCopyConstructible {
private: NotCopyConstructible (NotCopyConstructible const&) {}
};

that is not copy constructible does not prevent vectors from being used for
classes that do indeed meet the requirement.


So, when writing a template, the programer has to do the type checking
himself and not leave it to the compiler, as something like

template<tType> class Fii{
fun(tType T){
int f=T;
}
}

would compile (tType may be a int), but not run correctly if there is no
possability to cast tType to a int (if I understood you correctly) ?
Probably a good solution to this problem would be to provide a template that
will make it easier for users to define operator>> for their classes, if
this is possible. In the general case where an object is "deep" this may be
quite difficult, but if the class has no pointers or references it is very
easy.

HTH,
Kevin.


I'd appreachiate an example of how to do this (a quick sketch would be
perfect). In case it would make it easier, I send along a piece of my
code (a simplified version). Here, I have the class CartesVector that
currently works with InFile with InFile::operator>>(CartesVector & cv)
as a member of InFile. How would a template for operator>> look like in
this case?

regards
hall

// code:

// -------------- class InFile ---------------------------------
class InFile{
std::ifstream iF; ///< wrapped ifstream object

bool initInFile(std::string &); // opens a file

public:

// Constructor / Destructor
InFile(){}; // Default constructor,
~InFile();

bool open(std::string fName); // opens file fName for reading
bool close(); // closes current file.

// Operator >>
InFile& operator>>(int&); // integers
InFile& operator>>(short int&);
InFile& operator>>(CartesVector &); // homegrown class

};

// -------------- class CartesVector------------------------------
class CartesVector{
float x,y,z;
public:
void set(float ix,float iy,float iz){
x=ix; y=iy; z=iz;
}
}

// IMPLEMENTATIONS
-----------------------------------------------------------------------------

InFile::~InFile(){ // DESTRUCTOR
iF.close();
};

bool InFile::open(std::string fName){ // open(string n)
bool ret = initInFile(fName); return ret;
}

bool InFile::close(){ // close
iF.close(); return true;
}
// Opens file and initiates object for read
// There is a lot more to this function, but the important part is this:
bool InFile::initInFile(string &fName){
iF.open(fName.c_str(), ios::binary);
return true;
};

// Operator >>

InFile& InFile::operator>>(int &i){
iF.read((char*)&i , sizeof(int));
return *this;
};

InFile& InFile::operator>>(short int &i){
iF.read((char*)&i , sizeof(short int));
return *this;
};

InFile& InFile::operator>>(CartesVector & cv){ // operator>> for
float x,y,z; // CartesVector
iF.read((char*)&x, sizeof(float)); ok=iF;
iF.read((char*)&y, sizeof(float)); ok=(iF&&ok);
iF.read((char*)&z, sizeof(float)); ok=(iF&&ok);
cv.set(x,y,z);
return *this;
};

--
( - Remove capital X from email to reply - )

Jul 19 '05 #3
"hall" <Xc***********@yahoo.se> wrote in message
news:3F**************@yahoo.se...
So, when writing a template, the programer has to do the type checking
himself and not leave it to the compiler, as something like

template<tType> class Fii{
fun(tType T){
int f=T;
}
}

would compile (tType may be a int), but not run correctly if there is no
possability to cast tType to a int (if I understood you correctly) ?
The compiler does the type checking for you, but each template is basically
compiled for each type parameter. So here,

Fii <short> bir; // is fine
Fii <string> biz; // compiler error
I'd appreachiate an example of how to do this (a quick sketch would be
perfect). In case it would make it easier, I send along a piece of my
code (a simplified version). Here, I have the class CartesVector that
currently works with InFile with InFile::operator>>(CartesVector & cv)
as a member of InFile. How would a template for operator>> look like in
this case?


Actually, I had forgotten your requirement for both text-style and
binary-style io. Of course, it is the binary case that is trivial for cases
w/o pointers and references:

template <class T>
InFile& InFile::operator>>(T& item)
{
iF.read (reinterpret_cast <char*> (&item), sizeof (T));
return *this;
};

For text-based io, you can require your users to manually overload the standard stream extraction and insertion operators which is quite a reasonable requirement for this kind of thing. For your CartesVector class, this amounts to (something like):

class CartesVector{
float x,y,z;
friend std::istream& operator>> (std::istream&, CartesVector&);
friend std::ostream& operator<< (std::ostream&, CartesVector&);
public:
// ...
}

std::istream& operator>> (std::istream& is, CartesVector& cv)
{
return is >> cv.x >> cv.y >> cv.z;
}

std::ostream& operator<< (std::ostream&, CartesVector&)
{
return os << cv.x << cv.y << cv.z; //
may need to add spaces.
}

HTH.
Kevin
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ma740988 | last post by:
Consider the template member function that generates a random number within a range. #include <cstdlib> #include <ctime> #include <iostream> template<typename T> T Random(T bottom, T top) {
1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
5
by: mrstephengross | last post by:
Ok, I've got code that looks something like this: ================================================== template<typename T1, typename T2> class Base { public: explicit Base(const T1 & t1) {...
3
by: Bob Altman | last post by:
Hi all, This is the first time I've tried to create a templatized function, and I can't get past a linker error. I create a new, unmanaged C++ application and added a class named MyClass. In...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
1
by: tavianator | last post by:
If I had a class with a templatized constructor, like this: class foo { public: template<typename T> foo(); };
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
2
by: mrstephengross | last post by:
Hi folks! I'm trying to create a function pointer to a templatized, static, overloaded member function. My class (Mgr) has two functions with the same name: "go". The first takes a T reference (T...
4
by: Zachary Turner | last post by:
Is it possible to declare a class with a templatized constructor? I was sure it was, but GCC is giving me errors when I try to do this. Most likely user error. Some example code is: class...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.