This class takes an object and can read or write it to a position in a file.
The object must have copy constructor and operator= overloaded and ostream operator<< for printing.


#ifndef _TFILE_H_
#define _TFILE_H_
#include <fstream>

using namespace std;

template <class T>
class item{
	T data;
public:
	item(T d);
	item(char* str, int i);
	virtual ~item();
	void print(ostream &os) const;
	void read(char* str, int i = 0);
	void write(char* str,int i = -1);
};

template <class T>
item<T>::item(T d){
	data = d;
}

template <class T>
item<T>::item(char* str, int i){
	read(str, i);
}

template <class T>
item<T>::~item(){
}

template <class T>
void item<T>::print(ostream &os) const{
	os << data;
}

template <class T>
void item<T>::read(char* str, int i){
	fstream file(str, ios::in | ios::binary);
	file.seekg((ios::pos_type)(i*sizeof(data)));
	file.read((char*)&data,sizeof(data));
}

template <class T>
void item<T>::write(char* str, int i){
	fstream file;
	if (i == -1){
		file.open(str, ios::app | ios::out | ios::binary);
	}else{
		file.open(str, ios::in | ios::out | ios::binary);//ios::out call alone overwrites the entire file
	}
	if (!file){
		file.open(str, ios::out | ios::binary);
	}
	file.seekp((ios::pos_type)(i*sizeof(data)));
	file.write((const char*)&data, sizeof(data));
}

template <class T>
ostream& operator<< (ostream &os, const item<T> i){
	i.print(os);
	return os;
}


#endif


A class that stores an integer in bits.

bit.h:

#ifndef YZZ_BIT_H
#define YZZ_BIT_H
#include <iostream>

class bit{
   bool* _bits;
public:
   bit(int num);
   virtual ~bit();
   bool operator[] (const int i) const;
   bool& operator[] (const int i);
};

std::ostream & operator<<(std::ostream &os, const bit& b);

#endif

bit.cpp:

#include "bit.h"
bit::bit(int num){
   int i = sizeof(int)*8;
   _bits = new bool[i];
   for (;i--;_bits[i] = num & 1 << i){}
}

bool bit::operator[] (const int i) const{
   return _bits[i];
}

bool& bit::operator[] (const int i){
   return _bits[i];
}

bit::~bit(){
   delete[] _bits;
}

std::ostream & operator<<(std::ostream &os, const bit& b){
   for (int i = sizeof(int)*8;i--;os << !!b[i]){}
   return os;
}

    CMenuItem(const CMenuItem &CM);
  1. Passes CM to CField and Initializes the Label with CM
  2. Sets the _selected to _selected of CM
  3. Sets CFields::_data to the address of _format
  4. Sets the Label’s frame to this object.

CField does not have a copy constructor so is my only way of passing CM to CField something like this?

CMenuItem::CMenuItem(const CMenuItem &CM)
                  :CField(CM._row, CM._col, CM._width, CM._height, 
                             CM.data(), CM.visible(), CM._border), 
                  Label(CM.Label){

}

This this right for step one?

Is there an easier way of doing things?

CM.data() returns a void pointer to the _data of CM so how would I create a new version of whatever is at _data for the deep copy? do I need to? is the pointer just a member and does not actually contain data for CField so deep copy is not needed?