0

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);
}

Edited by Skeen: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by dusktreader
0

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

...
    CharBool = alfa; // Missing the possiblility to do CharBool[Number] = bool
                     // and the ability to do; CharBool[Number] = true (or false for that matter).

Boolean.cpp

...
bool Boolean::operator[] (char nIndex)
{
if (nIndex>7){/*SENT ERROR MESSEGE*/return 0;}
return ((*Bool >> nIndex) & 1);
}
...

I don't think you'll have much luck getting b=true to work very well. You need to understand what is happening under the hood first. When you call operator[], you should be getting back a reference to some value. There are no values available that are smaller than a sigle byte. A character is a sigle byte. Therefore, you can't possible fetch a reference to the ith bit in a character. The best you could do is get a reference to the whole character. You can't literally assign a value to a single bit. Instead, you have to play tricks like using the bitwise or operator with an l-shfted 1 to set the bit. You could possibly hack this in a certain way. Include some private variable that would keep the index of the last requested bit, then, in the assignment operator, set that bit. THIS IS A HACK so using the operator[] to access the individual bits for readin won't work. Instead, you should have one operator such as () for accessing the values of the bits and one operator [] for setting the values of the bits. Note that calling b will actually give you a reference to b, so you need to be careful.

Here's a compilable example:

#include <iostream>
using namespace std;

class Byte{
private:
    char val;  // No need to dynamically allocate this.  Char's are only a byte in size, so plenty of room on the stack.
    int p;     // The previous index.  Used for operator[] assignment
public:
    Byte(){
        val = 0;
    }

    Byte( const char val ){
        this->val = val;
    }

    Byte( const Byte& other ){
        this->val = other.val;
    }

    bool operator()( int i ){  // Use this for reading the ith bit
        return val >> i & 1;
    }

    Byte& operator[]( int i ){
        p = i;         // Remember which bit was requested
        return *this;
    }

    Byte& operator=( bool b ){
        val =  val | b << p;  // Set the pth bit.  Remember that p was set with operator[]
        return *this;
    }
};

int main(int argc, char *argv[]){
   Byte b;
   bool t;
   cout << "Byte intialized to 0" << endl;
   for( int i=7; i>=0; i-- ) cout << b(i);  // notice we use operator() to access the ith bit for reading
   cout << endl;
   b[3] = true;     // set the ith bit to true
   cout << "Byte intialized to b[3]=true" << endl;
   for( int i=7; i>=0; i-- ) cout << b(i);
   cout << endl;
    return 0;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.