C++ : 类 类名 :: 引用 { ... }
我正在学习 stl,从未见过像 class classname :: reference {}
这样的类,
我在网上搜索但可以获得很好的信息..
class bitset::reference {
friend class bitset;
reference(); // no public constructor
public:
~reference();
operator bool () const; // convert to bool
reference& operator= ( bool x ); // assign from bool
reference& operator= ( const reference& x ); // assign from bit
reference& flip(); // flip bit value
bool operator~() const; // return inverse value
};
- 这里的 :: reference 是做什么用的?
我在这里看到了这段代码[在此处输入链接描述][1] http://www.cplusplus.com/reference/stl/bitset/ 我以前从事过c++工作。
I was learning stl and never saw this kind of class like class classname :: reference {}
I searched on the net but could get good information ..
class bitset::reference {
friend class bitset;
reference(); // no public constructor
public:
~reference();
operator bool () const; // convert to bool
reference& operator= ( bool x ); // assign from bool
reference& operator= ( const reference& x ); // assign from bit
reference& flip(); // flip bit value
bool operator~() const; // return inverse value
};
- what is this :: reference here for ?
i saw this code here[enter link description here][1]
http://www.cplusplus.com/reference/stl/bitset/
i have worked on c++ before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您查看了 bitset 类定义吗?某处有这样的事情:
这很像将函数的主体放在类主体之外。现在我们将嵌套类的主体放在父类之外:
顺便说一下,在 MSVC (
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\bitset
) 中,有实际上是在彼此内部定义的:与 g++ 的
bitset.h
相同,尽管稍微复杂一些。Did you look at the bitset class definition? There is something like this somewhere:
It's much like putting the body of a funciton outside the class body. Now we are putting the body of a nested class outside of the parent class:
By the way, in MSVC (
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\bitset
) the're actually defined inside each other:It's the same for g++'s
bitset.h
, although a bit more complex.引用是类名,没什么特别的。 bitset::reference 表示引用是内部类。
reference is class name, nothing special. bitset::reference means that reference is internal class.
您引用的代码片段之前的行解释道:
。C++ 不允许引用位域,因此使用
reference
类来模拟它。The line right before the snippet you quoted explains:
C++ does not allow references to bitfields, so the
reference
class is used to simulate it.它是一个嵌套类。来自文章:
另一种解释是,
bitset
类不仅用作类,还用作命名空间。It is a nested class. From the article:
An alternative interpretation is that the
bitset
class is being used not only as a class but as a namespace as well.