Archive

Monthly Archives: November 2013

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?