C++ : 类 类名 :: 引用 { ... }

发布于 2024-12-12 08:31:32 字数 841 浏览 0 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫言歌 2024-12-19 08:31:32

您查看了 bitset 类定义吗?某处有这样的事情:

template<size_t _Bits>
class bitset
{
    ...
    class reference;
    ...
}

这很像将函数的主体放在类主体之外。现在我们将嵌套类的主体放在父类之外:

class bitset::reference
{
    /* class body */
}

顺便说一下,在 MSVC (C:\Program Files\Microsoft Visual Studio 9.0\VC\include\bitset) 中,有实际上是在彼此内部定义的:

// TEMPLATE CLASS bitset
template<size_t _Bits>
class bitset
{   // store fixed-length sequence of Boolean elements
typedef unsigned long _Ty;  // base type for a storage word
enum {digits = _Bits};  // extension: compile-time size()

public:
typedef bool element_type;  // retained

    // CLASS reference
class reference
    {   // proxy for an element
    friend class bitset<_Bits>;
    .
    .
    .

与 g++ 的 bitset.h 相同,尽管稍微复杂一些。

Did you look at the bitset class definition? There is something like this somewhere:

template<size_t _Bits>
class bitset
{
    ...
    class reference;
    ...
}

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:

class bitset::reference
{
    /* class body */
}

By the way, in MSVC (C:\Program Files\Microsoft Visual Studio 9.0\VC\include\bitset) the're actually defined inside each other:

// TEMPLATE CLASS bitset
template<size_t _Bits>
class bitset
{   // store fixed-length sequence of Boolean elements
typedef unsigned long _Ty;  // base type for a storage word
enum {digits = _Bits};  // extension: compile-time size()

public:
typedef bool element_type;  // retained

    // CLASS reference
class reference
    {   // proxy for an element
    friend class bitset<_Bits>;
    .
    .
    .

It's the same for g++'s bitset.h, although a bit more complex.

鲸落 2024-12-19 08:31:32

引用是类名,没什么特别的。 bitset::reference 表示引用是内部类。

reference is class name, nothing special. bitset::reference means that reference is internal class.

浅笑依然 2024-12-19 08:31:32

您引用的代码片段之前的行解释道:

由于大多数 C++ 环境中不存在如此小的元素类型,因此各个元素将作为模仿 bool 元素的特殊引用进行访问

。C++ 不允许引用位域,因此使用 reference 类来模拟它。

The line right before the snippet you quoted explains:

Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements

C++ does not allow references to bitfields, so the reference class is used to simulate it.

弥繁 2024-12-19 08:31:32

它是一个嵌套类。来自文章:

一个类可以在另一个类的范围内声明。这样一个
类称为“嵌套类”。嵌套类被认为是
在封闭类的范围内并且可供使用
那个范围之内。从范围以外引用嵌套类
由于其直接封闭范围,您必须使用完全限定名称。

另一种解释是,bitset 类不仅用作类,还用作命名空间。

It is a nested class. From the article:

A class can be declared within the scope of another class. Such a
class is called a "nested class." Nested classes are considered to be
within the scope of the enclosing class and are available for use
within that scope. To refer to a nested class from a scope other than
its immediate enclosing scope, you must use a fully qualified name.

An alternative interpretation is that the bitset class is being used not only as a class but as a namespace as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文