Archived

This topic is now archived and is closed to further replies.

Jiia

Template Question

Recommended Posts

This class is not this simple, but I've removed everything to keep it simple.
class KFileIO : public fstream
{
public:
	template < class Type >
	VOID Save(Type *Data,LONG Count=1)
		{ write((BYTE*)Data,sizeof(Type)*Count); }
	template < class Type >
	VOID Load(Type *Data,LONG Count=1)
		{ read((BYTE*)Data,sizeof(Type)*Count); }
};
.. then later in code..
struct KCell
{
	LONG X,Y;
	LONG W,H;
};
VOID Map::Save(...)
{
	KCell Cell;
	KFileIO File;
	File.Open(...);
	...
	File.Save(&Cell);
	...
	File.Close();
}  
Everything seems to be saving/loading correctly, but I've messed with more than one thing at a time, and I'm not positive this is not the problem. The compiler is fine with finding the type of a pointer, right? Just because I pass the address of the structure, it doesn't prevent the compiler from creating the correct version of the function? And this does create just a version of the function for the call, right? Not a version of the class?! Thanks much for any help! -Jiia [edited by - Jiia on October 2, 2002 10:04:55 PM]

Share this post


Link to post
Share on other sites
* use std::fstream
* pass a reference for Load, const reference for Save
* size parameters should be of type size_t (C++ Standard type for object sizes)
* use a static_cast instead of a C cast.
* Whenever possible, use aggregation, not inheritance, unless you know the semantics of the class... It is HARD to properly inherit from the concrete stream classes.


    
#include <fstream> // NOT <fstream.h>



class KFileIO : public std::fstream // Not fstream

{
public:
template < class Type >
void Save(const Type& Data, size_t Count=1)
{ write(static_cast<const char*>(&Data),sizeof(Type)*Count); }
template < class Type >
void Load(Type& Data,size_t Count=1)
{ read(static_cast<char*>(&Data),sizeof(Type)*Count); }
};
.. then later in code..
struct KCell
{
LONG X,Y;
LONG W,H;
};
VOID Map::Save(...)
{
KCell Cell;
KFileIO File;
File.Open(...);
...
File.Save(Cell);
...
File.Close();
}


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 2, 2002 10:13:40 PM]

Share this post


Link to post
Share on other sites
Hey, thanks for the fast reply.

What exactly is a static_cast? I''ve never heard of it. What is the difference? Why wouldn''t all casts be static?

The size parameters you mention don''t exist. It''s a count. A number of objects in a list or array. Not an object size.

And last, std::fstream is another I''ve not heard. Is this a version of fstream before it was modified from it''s standard? Something close to that?

You didn''t mention what this would fix/change, if anything, and avoided my actual questions.

Ha ha, I''m not complaining. Thanks much for the info.
-Jiia

Share this post


Link to post
Share on other sites
quote:
Original post by Jiia
What exactly is a static_cast? I''ve never heard of it. What is the difference? Why wouldn''t all casts be static?


C++ has four different types of casts :

- static_cast(value), roughly equivalent to a C-style cast, except when pointers are involved.
- dynamic_cast(value), used for safe downcasting from a Base class to a Derived class. returns NULL (pointer) or throws a std::bad_cast (reference) if the object you have is not really a Derived object.
- reinterpret_cast(value), converts the variable as an amorphous bit pattern (i.e. change the type without affecting the binary representation)
- const_cast(value), used to remove const or volatile qualifiers (turn a const foo into a foo).

Interestingly, though those are compiler built-in operations, the syntax itself is regular and can be extended by user functions :

template<class T> fruny_cast( T& value );

Some libraries (e.g. boost - see link in sig) use that to their advantage, defining new types of casts (e.g. lexical_cast, convert a string to a number, and conversely).

The size parameters you mention don''t exist. It''s a count. A number of objects in a list or array. Not an object size.
quote:

Ok. Note that size_t is the type returned by sizeof() though.
[quote]
And last, std::fstream is another I''ve not heard. Is this a version of fstream before it was modified from it''s standard? Something close to that?


fstream, declared in <fstream.h> is a leftover from pre-standard days. It is deprecated, unsupported, quirky, unportable, flaky, and a pain in the ass to use.

std::fstream is declared in <fstream> is the standard C++ version of said class. It''s interface is slightly different (but a good manual will cover it), but it is easier to use, and has been designed better. <iostream> and <iostream.h> are in the same case.

And not the two headers are not equivalent, don''t let the <stdio.h> / <cstdio> pattern fool you.
quote:

You didn''t mention what this would fix/change, if anything, and avoided my actual questions.


Some compilers (VC) do not like trying to figure out the template type (Type) from a pointer (Type*) overmuch (I''ve had trouble with it at times). Plus it is cleaner to use a reference in the function interface, and convert it to a pointer internally.
quote:

Ha ha, I''m not complaining. Thanks much for the info.


You''re most welcome



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites