我有以下类,它应该代表一个 8 位有符号字符。
class S8
{
private:
signed char val;
public:
S8 & operator=(const signed char other)
{
if ((void*)this != (void*)&other)
{
val = other;
}
return *this;
}
operator signed char() {signed char i; i = (signed char) val; return i;}
void write (OutputArray & w)
{
/* This function is the whole purpose of this class, but not this question */
}
};
但是,当我将负数分配给它的一个对象时,
S8 s;
char c;
s = -4;
c = -4;
printf("Results: %d, %s\n",s,c);
我从 printf 中得到“结果:252,-4”。有没有办法修改类,这样的情况下会看到有符号字符的行为,而不是我得到的无符号字符行为?
谢谢!