So I'm working on this class, which is basically to wrap a char into 8bools, and it works so far, however I'm running into a problem, in the dedication of values, since I'm to use the overloaded array operator, as well as the overloaded assignment operator, at once, however I can't really find the way to do this; this is my code:
Main.cpp
#include "Boolean.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Boolean CharBool(0);
bool alfa;
for (int x=7; x>=0; x--)
{
alfa = CharBool[x];
cout << alfa;
}
cout << endl;
alfa = true;
CharBool = alfa; // Missing the possiblility to do CharBool[Number] = bool
// and the ability to do; CharBool[Number] = true (or false for that matter).
for (int x=7; x>=0; x--)
{
alfa = CharBool[x];
cout << alfa;
}
cin.get();
return 0;
}
Boolean.h
#ifndef BOOLEAN_H
#define BOOLEAN_H
class Boolean
{
public:
Boolean();
Boolean(char PresetValue);
~Boolean();
bool operator[] (char nIndex);
void operator= (bool &b);
private:
char *Bool;
protected:
};
#endif
Boolean.cpp
#include "Boolean.h"
Boolean::Boolean()
{
Bool = new char;
*Bool = 0;
}
Boolean::Boolean(char PresetValue)
{
Bool = new char;
*Bool = PresetValue;
}
Boolean::~Boolean()
{
delete Bool;
}
void Boolean::operator=(bool &b) // This needs to understand the [] overloading, how is that possible?
{
*Bool = b;
}
bool Boolean::operator[] (char nIndex)
{
if (nIndex>7){/*SENT ERROR MESSEGE*/return 0;}
return ((*Bool >> nIndex) & 1);
}