C++ Overload Operator [ ][ ]

Operator overloading in C

C does not support operator overloading (beyond what it built into the language).

Is it possible to overload operators in C?

No, it is not possible. C does not support operator overloading by the developer.

C++ overloading == operator example

In the line

if (board[y][x] == squerState::ROOK)

it looks like the type of the expression on the left-hand side is Board*, but the type of expression on the right-hand side is enum squerState. You did not define an equals operator for those types and they can't be compared by other means.

You probably want your Board class to contain an array of SquareState instances.

enum Piece { None, Pawn, ... };
enum Color { Black, White };

struct SquareState
{
Piece piece_;
Color color_;
};

struct Board
{
void drawBoard() const;
SquareState board_[8][8];
};

void Board::drawBoard() const
{
for( int i = 0; i < 8; ++i )
{
for( int j = 0; j < 8; ++j )
{
switch( board_[ i ][ j ].piece_ )
{
case Piece::None:
std::cout << " ";
break;
case Piece::Pawn:
std::cout << "P";
break;
// ...
}
}
std::cout << std::endl;
}
}

I hope this helps.

C++ operator overloading [] and return types

In this declaration

Vec *a = new Vec(2, avals);

there is declared a pointer of the type Vec *. So an expression with the dereferenced pointer has the type Vec.

So in this statement

cout << a[0] << endl;

the expression a[0] has the type Vec.

It seems you mean

( *a )[0]

or

a[0][0]

c++ overload operator() for assigning value in a dynamic 2D array

Your first overload must be in form:

int operator() (int row, int col) const

Not

const int operator() (int row, int col)

And it is not the read operation, it is used when an object of your type is created as const, this overload will be used, if not const, other overload will be used, both for reading and writing.

Combined Operator Overloading in C++?

No, you can't overload on the return type, or on the context in which the call appears (e.g. in an assignment etc).

You could, however, return a proxy object that would have a bunch of overloaded conversion operators. Without seeing what you intend to do, it's hard to say how far you might be able to get with this approach, or whether it's even a sane thing to do.

Number of arguments in operator overload in C++

Suppose you have a class like this:

class Element {
public:
Element(int value) : value(value) {}
int getValue() const { return value; }
private:
int value;
};

There are four ways to define a binary operator such as +.

  1. As a free function with access to only the public members of the class:

    // Left operand is 'a'; right is 'b'.
    Element operator+(const Element& a, const Element& b) {
    return Element(a.getValue() + b.getValue());
    }

    e1 + e2 == operator+(e1, e2)

  2. As a member function, with access to all members of the class:

    class Element {
    public:
    // Left operand is 'this'; right is 'other'.
    Element operator+(const Element& other) const {
    return Element(value + other.value);
    }
    // ...
    };

    e1 + e2 == e1.operator+(e2)

  3. As a friend function, with access to all members of the class:

    class Element {
    public:
    // Left operand is 'a'; right is 'b'.
    friend Element operator+(const Element& a, const Element& b) {
    return a.value + b.value;
    }
    // ...
    };

    e1 + e2 == operator+(e1, e2)

  4. As a friend function defined outside the class body—identical in behaviour to #3:

    class Element {
    public:
    friend Element operator+(const Element&, const Element&);
    // ...
    };

    Element operator+(const Element& a, const Element& b) {
    return a.value + b.value;
    }

    e1 + e2 == operator+(e1, e2)



Related Topics



Leave a reply



Submit