LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 179|回复: 0

嵌套类中的成员函数

[复制链接]
发表于 2024-2-3 23:13:08 | 显示全部楼层 |阅读模式
在嵌套类中声明的成员函数可在文件范围中定义。 前面的示例可能已编写:

// member_functions_in_nested_classes.cpp
class BufferedIO
{
public:
    enum IOError { None, Access, General };
    class BufferedInput
    {
    public:
        int read(); // Declare but do not define member
        int good(); //  functions read and good.
    private:
        IOError _inputerror;
    };

    class BufferedOutput
    {
        // Member list.
    };
};
// Define member functions read and good in
//  file scope.
int BufferedIO::BufferedInput::read()
{
   return(1);
}

int BufferedIO::BufferedInput::good()
{
    return _inputerror == None;
}
int main()
{
}
在前面的示例中,qualified-type-name 语法用于声明函数名称。 声明:

BufferedIO::BufferedInput::read()
表示“作为 BufferedIO 类范围内的 BufferedInput 类的成员的 read 函数”。由于此声明使用 qualified-type-name 语法,因此以下形式的构造是可能的:

typedef BufferedIO::BufferedInput BIO_INPUT;

int BIO_INPUT::read()
上述声明与前一个声明等效,但它使用了 typedef 名称来代替类名称。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表