std::ios_base::pword
科技促进发展:监督办[医]基::pword
void*& pword( int index | | |
---|
首先,分配或调整私有存储%28动态数组的大小。void*
或另一个可索引的数据结构%29足以使index
一个有效的索引,然后返回对void*
元素的私有存储的索引。index
...
对此的任何操作都可能使引用无效。ios_base
对象,包括对pword()
,但是保留存储的值,以便从pword(index)
使用相同的索引,以后将产生相同的值%28,直到下一次调用copyfmt()
29%。该值可用于任何目的。元素的索引必须由xalloc()
的其他用户之间的冲突。ios_base
可能会发生。新元素初始化为NULL
...
如果分配失败,调用std::basic_ios<>::setstate(badbit)可能会std::ios_base::failure...
参数
index | - | index value of the element |
---|
返回值
对元素的引用。
例外
可抛std::ios_base::failure
当设置坏位时。
注记
中存储的指针pword
需要管理,register_callback()
可用于安装处理程序,根据需要执行深度复制或取消分配。
例
将基类pword存储用于派生流对象的运行时类型标识。
二次
#include <iostream>
template<class charT, class traits = std::char_traits<charT> >
class mystream : public std::basic_ostream<charT, traits>
{
public:
static const int xindex;
mystream(std::basic_ostream<charT, traits>& ostr) :
std::basic_ostream<charT, traits>(ostr.rdbuf())
{
this->pword(xindex) = this;
}
void myfn()
{
*this << "[special handling for mystream]";
}
};
// each specialization of mystream obtains a unique index from xalloc()
template<class charT, class traits>
const int mystream<charT, traits>::xindex = std::ios_base::xalloc(
// This I/O manipulator will be able to recognize ostreams that are mystreams
// by looking up the pointer stored in pword
template<class charT, class traits>
std::basic_ostream<charT,traits>& mymanip(std::basic_ostream<charT,traits>& os)
{
if (os.pword(mystream<charT,traits>::xindex) == &os)
static_cast<mystream<charT,traits>&>(os).myfn(
return os;
}
int main()
{
std::cout << "cout, narrow-character test " << mymanip << '\n';
mystream<char> myout(std::cout
myout << "myout, narrow-character test " << mymanip << '\n';
std::wcout << "wcout, wide-character test " << mymanip << '\n';
mystream<wchar_t> mywout(std::wcout
mywout << "mywout, wide-character test " << mymanip << '\n';
}
二次
产出:
二次
cout, narrow-character test
myout, narrow-character test [special handling for mystream]
wcout, wide-character test
mywout, wide-character test [special handling for mystream]
二次
另见
iword | resizes the private storage if necessary and access to the long element at the given index (public member function) |
---|---|
xalloc static | returns a program-wide unique integer that is safe to use as index to pword() and iword() (public static member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。