I have two wrapper classes for string and int and one to represent Binary Operation and overloading the operator <<
to write them in a string format. Unfortunately, I can't overload <<
without friend
function and I can't get polymorphism without virtual
and can't use friend
with virtual
!!
#include <iostream>
#include <string>
class AST {};
class expr: public AST {};
class operator_: public AST {};
class BinOp: public expr {
private:
expr *_left;
expr *_right;
operator_ *_op;
public:
BinOp(expr *p_left, expr *p_right, operator_ *p_op):_left(p_left),_right(p_right),_op(p_op) {}
friend std::ostream &operator<< (std::ostream &out, const BinOp &binop) {
out << (binop._left) << " " << (binop._op) << " " << (binop._right);
}
};
class LShift: public operator_ {
public:
LShift() {}
friend std::ostream &operator<< (std::ostream &out, const LShift &lshift) {
out << "<<";
}
};
class Name: public expr {
private:
std::string _id;
public:
Name(std::string p_id) { _id = p_id; }
friend std::ostream &operator<< (std::ostream &out, const Name &name) {
out << name._id;
}
};
class Num: public expr {
private:
int n;
public:
Num(int p_n) { n = p_n; }
friend std::ostream &operator<< (std::ostream &out, const Num &num) {
out << num.n;
}
};
int main() {
auto name = Name("x");
auto num = Num(5);
auto lshift = LShift();
auto binop = BinOp(&name, &num, &lshift);
std::cout << binop << '\n';
return 0;
}
So the program mentioned above outputs the pointer,
0x7ffd05935db0 0x7ffd05935d8b 0x7ffd05935d8c
but instead of a pointer, I need the objects needed to print.
x << 5