如何重载 << 运算符,使其只影响 class 的成员?
How to overload the << operator so it only affects a member of a class?
我有一个 class Bitset
存储 vector
个字符,我希望能够在我使用 cout << char
时转换那个 char
仅当它是 class.
的一部分时才转换为 short int
代码:
template<long long X>
class Bitset
{
public: std::vector<unsigned char> bit = std::vector<unsigned char> ((X+7)/8);
public:
/* constructors */
friend std::ostream &operator<< (std::ostream &output, const char x);
};
std::ostream &operator<< (std::ostream &output, const char x)
{
output<<(short)(x);
return output;
}
我的想法是,如果我写:
Bitset a;
/* code */
cout << a.bit[x];
cout << 'a';
我想将 a.bit[x]
转换成短片,但也不想 'a'
。
您不能重载 operator<<
以使 char
以您想要的方式运行。它不知道 char
从哪里来,所以它不能根据来源做出不同的行为。
要按照您想要的方式进行这项工作,您必须Bitset
实现它自己的operator[]
,returns一个代理对象,然后您可以为该代理重载 operator<<
,例如:
template<long long X>
class Bitset
{
private:
std::vector<unsigned char> bits = std::vector<unsigned char> ((X+7)/8);
public:
/* constructors */
class BitProxy
{
private:
unsigned char &bit;
public:
BitProxy(unsigned char &bit) : bit(bit) {}
BitProxy& operator=(unsigned char x) { bit = x; return *this; }
operator unsigned char() const { return bit; }
};
BitProxy operator[](size_t index) { return BitProxy(bits[index]); }
friend std::ostream& operator<< (std::ostream &output, const BitProxy &x)
{
output << static_cast<short>(static_cast<unsigned char>(x));
return output;
}
};
Bitset a;
// populate a as needed...
cout << a[x];
cout << 'a';
我会采用一种非常简单的方法:只需在您的 class 中使用以下签名实现一个函数:
short GetElementAsShort (size_t index);
然后你当然可以这样做:
Bitset <128> a;
std::cout << a.GetElementAsShort (42) << "\n";
我真的不明白为什么需要更复杂的东西。这也让您在阅读代码时一目了然。
我有一个 class Bitset
存储 vector
个字符,我希望能够在我使用 cout << char
时转换那个 char
仅当它是 class.
代码:
template<long long X> class Bitset { public: std::vector<unsigned char> bit = std::vector<unsigned char> ((X+7)/8); public: /* constructors */ friend std::ostream &operator<< (std::ostream &output, const char x); }; std::ostream &operator<< (std::ostream &output, const char x) { output<<(short)(x); return output; }
我的想法是,如果我写:
Bitset a; /* code */ cout << a.bit[x]; cout << 'a';
我想将 a.bit[x]
转换成短片,但也不想 'a'
。
您不能重载 operator<<
以使 char
以您想要的方式运行。它不知道 char
从哪里来,所以它不能根据来源做出不同的行为。
要按照您想要的方式进行这项工作,您必须Bitset
实现它自己的operator[]
,returns一个代理对象,然后您可以为该代理重载 operator<<
,例如:
template<long long X>
class Bitset
{
private:
std::vector<unsigned char> bits = std::vector<unsigned char> ((X+7)/8);
public:
/* constructors */
class BitProxy
{
private:
unsigned char &bit;
public:
BitProxy(unsigned char &bit) : bit(bit) {}
BitProxy& operator=(unsigned char x) { bit = x; return *this; }
operator unsigned char() const { return bit; }
};
BitProxy operator[](size_t index) { return BitProxy(bits[index]); }
friend std::ostream& operator<< (std::ostream &output, const BitProxy &x)
{
output << static_cast<short>(static_cast<unsigned char>(x));
return output;
}
};
Bitset a;
// populate a as needed...
cout << a[x];
cout << 'a';
我会采用一种非常简单的方法:只需在您的 class 中使用以下签名实现一个函数:
short GetElementAsShort (size_t index);
然后你当然可以这样做:
Bitset <128> a;
std::cout << a.GetElementAsShort (42) << "\n";
我真的不明白为什么需要更复杂的东西。这也让您在阅读代码时一目了然。