I have struct Token, to which I'm trying assign operator=. I wanna be able to assign to char. I tried char operator=(const Token& token){ return token.kind; }, which throws error, says is not unary operator, tried char operator=(const char& ch, const Token& token){ return token.kind; } didn't help either. Yes I can do just char ch { token.kind };, but I wanna do it through operator in case if add some logic. Can you help me?
struct Token { char kind; int value; Token(char kind, int value): kind(kind), value(value){}: }
Token::operator char() const;in in your token class. Beware this can give unexpected results. PreferToken::as_char() const;instead.Tokenthe power to decide how the conversion should be performed is reasonable, so a conversion operator makes some sense... but at least make itexplicit!