beard@ux1.lbl.gov (Patrick C Beard) (10/04/88)
Hello. I am still getting my bearings in C++ and decided that as a first
attempt at class construction I would write a file class of a slightly
different flavor than the ostream model presented in Stroustrap's book.
My model is that of an array of bytes:
#include <stdio.h>
#include <stream.h>
class file {
FILE *f; // pointer to a file variable
char buf[BUFSIZ];
char opened; // has file been opened?
long _where;
int error;
public:
file(char*, char*);
~file(void);
char& operator[](long);
char operator=(char);
};
I can get to byte n of the file by specifying:
file f("foo","r+"); /* fopen style call */
long n=100000;
char c=f[n];
Now, what if I want to set the value of byte n? i.e.,
f[n]=c;
Do I make operator[] a friend of class file and write two versions,
one with class file as first argument, and one with second? Can what
I want to do be done?
Thanks,
Patrick Beard
Lawrence Berkeley Laboratory
beard@ux1.lbl.gov
keith@nih-csl.UUCP (keith gorlen) (10/05/88)
In article <1054@helios.ee.lbl.gov>, beard@ux1.lbl.gov (Patrick C Beard) writes: -> Hello. I am still getting my bearings in C++ and decided that as a first -> attempt at class construction I would write a file class of a slightly -> different flavor than the ostream model presented in Stroustrap's book. -> My model is that of an array of bytes: -> -> #include <stdio.h> -> #include <stream.h> -> -> class file { -> FILE *f; // pointer to a file variable -> char buf[BUFSIZ]; -> char opened; // has file been opened? -> long _where; -> int error; -> public: -> file(char*, char*); -> ~file(void); -> char& operator[](long); -> char operator=(char); -> }; -> -> I can get to byte n of the file by specifying: -> file f("foo","r+"); /* fopen style call */ -> long n=100000; -> char c=f[n]; -> -> Now, what if I want to set the value of byte n? i.e., -> f[n]=c; -> -> Do I make operator[] a friend of class file and write two versions, -> one with class file as first argument, and one with second? Can what -> I want to do be done? I use what I call a "private class" to do this sort of thing: class file; class FilePos { file* fp; long pos; FilePos(file* f, long where) { fp = f; pos = where; } friend FilePos file::operator[](long); public: void operator=(char c) { fseek(fp->f,pos,0); putc(c, fp->f); } operator const char() { return getc(fp->f); } }; In class file define: FilePos operator[](long i) { return FilePos(this,i); } When you write "char c = f[n]" the expression f[n] results in a FilePos, which C++ converts to a const char by using FilePos::operator const char(). When you write "f[n] = 'x'" the expression f[n] results in a FilePos, so C++ uses FilePos::operator=(char) to do the assignment. Note that class FilePos has no public constructors, so client programs can't create them. Only file::operator[](long) can. I think this is a really good use for friends that I mentioned during the recent discussion about friends in this newsgroup, but I don't think my posting got out because news posting was broken here. -- Keith Gorlen phone: (301) 496-5363 Building 12A, Room 2017 uucp: uunet!nih-csl!keith National Institutes of Health Internet: keith%nih-csl@ncifcrf.gov Bethesda, MD 20892