std::ios_base::xalloc
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
static int xalloc(); |
||
返回一个唯一的(程序范围内)的指数值,可用于访问一个long和void*元素,通过调用私有存储
iword()
pword()
。因此,要求xalloc
不分配内存目录 |
[编辑] 参数
(无)
[编辑] 返回值
独特的整数使用PWORD / iword指数
原文:
unique integer for use as pword/iword index
[编辑] 示例
衍生流对象的运行时类型识别使用基类PWORD的存储空间.
原文:
Uses base class pword storage for runtime type identification of derived stream objects.
#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]
[编辑] 另请参阅
调整大小的私人存储,如果必要的,访问void*元素在给定的索引 原文: resizes the private storage if necessary and access to the void* element at the given index (公共成员函数) | |
调整大小的私人存储,如果必要的,访问long元素在给定的索引 原文: resizes the private storage if necessary and access to the long element at the given index (公共成员函数) |