如何重载〜(按位不是)运算符?

时间:2012-03-05 03:02:36

标签: c++

我正在尝试使用~来翻转'二进制'类的位。这个'Binary'类存储一个char数组'0'和'1',名为bs。我只想翻转这个数组中的字符:

Binary& operator~ ()
{
    int i = 0;
    while( i < index ) {
        if (bs[i] == '1')
            bs[i] == '0';
        else bs[i] == '1';
        i++;
    }
    return *this;
}

但无论是否使用while或for,代码似乎都无法进入循环。也许我写错了。这是完整的代码:

#include <iostream>
#include <windows.h>

using namespace std;

TCHAR pressanykey(const TCHAR* prompt = NULL)
{
    TCHAR  ch;
    DWORD  mode;
    DWORD  count;
    HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);

    // Prompt the user
    if (prompt == NULL)
        prompt = TEXT("Press any key to continue...");

    WriteConsole(
      GetStdHandle(STD_OUTPUT_HANDLE),
      prompt,
      lstrlen(prompt),
      &count,
      NULL
    );

    // Switch to raw mode
    GetConsoleMode(hstdin, &mode);
    SetConsoleMode(hstdin, 0);

    // Wait for the user's response
    WaitForSingleObject(hstdin, INFINITE);

    // Read the (single) key pressed
    ReadConsole(hstdin, &ch, 1, &count, NULL);

    // Restore the console to its previous state
    SetConsoleMode(hstdin, mode);

    // Return the key code
    return ch;
}

class Binary {
    char *bs;
    int index;

    public:
        Binary(int x) {
            bs = new char[20];
            index = 0;
            DecimalToBinary(x);
            bs[index] = '\0';
        }

        Binary(char* str) {
            bs = str;
            index = 0;
            while (bs[index] != '\0') {
                index += 1;
            }
        }

        Binary(const Binary& original) {
            bs = original.bs;
            index = original.index;
        }

        ~Binary() {
            delete [] bs;
            bs = NULL;
        }

        int ToDecimal() {
            int result = 0;
            for (int i = 0; i < index; i++) {
                result *= 2;
                if (bs[i] == '1')
                    result += 1;
            }
            return result;
        }

        Binary& operator~ ()
        {
            int i = 0;
            while( i < index ) {
                if (bs[i] == '1')
                    bs[i] == '0';
                else bs[i] == '1';
                i++;
            }
            return *this;
        }

        Binary& operator= (const Binary &b) {
            delete [] bs;
            bs = NULL;

            bs = b.bs;
            index = b.index;

            return *this;
        }

        void DecimalToBinary(int number) {
            int remainder;

            if (number == 1) {
                bs[index] = '1';
                index += 1;
                return;
            }
            if (number == 0) {
                bs[index] = '0';
                index += 1;
                return;
            }

            remainder = number%2;
            DecimalToBinary(number >> 1);
            if (remainder == 1) {
                bs[index] = '1';
                index += 1;
                return;
            }
            if (remainder == 0) {
                bs[index] = '0';
                index += 1;
                return;
            }
        }

        friend istream& operator>>(istream& is, Binary& b);
        friend ostream& operator<<(ostream& os, Binary& b);
};

istream& operator>>(istream& is, Binary& b)
{
    char *str = new char[20];

    is >> str;

    b.bs = str;

    b.index = 0;
    while (b.bs[b.index] != '\0') {
        b.index += 1;
    }

    return is;
}

ostream& operator<<(ostream& os, Binary& b)
{
    os << b.bs;
    return os;
}

int main() {
    Binary a(15);
    Binary b("10110");
    Binary c(b);

    cout << "binary a is " << a << endl;
    cout << "binary b is " << b << endl;
    cout << "binary c is " << c << endl;

    cout << endl << "Re-enter binary b: ";
    cin >> b;

    cout << "binary a is " << a << endl;
    cout << "binary b is " << b << endl;
    cout << "binary c is " << c << endl;

    cout << "binary b in decimal form: " << b.ToDecimal() << endl;
    cout << "bit flips b: "<< ~b << endl;

    pressanykey();
    return 0;
}

1 个答案:

答案 0 :(得分:4)

您正尝试使用==而不是=分配值。