如何覆盖C ++标准库类函数? [英] How can I override an C++ standard-library class function?

查看:293
本文介绍了如何覆盖C ++标准库类函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何覆盖C ++标准库类函数?在我的应用程序中,我在许多不同的代码中使用 ofstream 对象。现在我想在Linux,Ubuntu中以不同的权限模式打开文件。但 函数没有参数来指定它创建的文件的权限模式。

How can I override a C++ standard-library class function? In my application, I use ofstream objects in many different places of code. And now I want to open files in a different permission mode in Linux, Ubuntu. But open function of ofstream has no parameter to specify the permission mode of the file it creats.

现在我想重写 open()函数 ofstream 将获得一个参数来指定用户访问的权限。

Now I want to override open() function of ofstream class so it will get a parameter to specify the permissions for user access.

推荐答案

对于初学者,为了澄清你的术语,STL通常指包含容器,迭代器和算法的C ++标准库的子集。流类是C ++标准库的一部分,但通常不与STL捆绑在一起。一些纯粹主义者将坚持认为在C ++标准库中没有诸如STL这样的东西(因为STL在技术上是一个被纳入标准的第三方库),但是大多数C ++程序员都知道你的意思。

For starters, to clarify your terminology, the STL usually refers to the subset of the C++ standard library containing the containers, iterators, and algorithms. The streams classes are part of the C++ standard library, but are usually not bundled together with the STL. Some purists will insist that there is no such thing as the STL in the C++ standard library (since the STL is, technically speaking, a third-party library that was adopted into the standard), but most C++ programmers will know what you mean.

至于你的问题,标准中没有办法指定 ofstream 的权限模式。如果你想创建自己的自定义流类,像 ofstream 但是支持权限,最好的办法是做以下操作:

As for your question, there is no way within the standard to specify permission modes with ofstream. If you want to create your own custom stream class that is like ofstream but which supports permissions, your best bet would be to do the following:

创建 basic_streambuf 的子类,允许在指定Unix时打开,写入和读取文件权限。流类被设计为使得与物理设备(如磁盘,网络和控制台)通信的细节都由 basic_streambuf 类及其派生类处理。

  1. Create a subclass of basic_streambuf that allows you to open, write, and possibly read files while specifying Unix permissions. The streams classes are designed so that the details of communicating with physical devices like disk, networks, and the console are all handled by the basic_streambuf class and its derived classes. If you want to make your own stream class, implementing a stream buffer would be an excellent first step.

定义您自己的类 basic_ostream 并安装您的自定义 basic_streambuf 。默认情况下, basic_ostream 支持所有的标准输出例程,通过底层的 basic_streambuf 对象实现它们。一旦你有自己的流缓冲区,构建使用 streambuf basic_ostream 类将导致所有标准流操作在该类(例如< )上开始对 streambuf 进行适当的调用。

Define your own class that subclasses basic_ostream and installs your custom basic_streambuf. By default, the basic_ostream supports all of the standard output routines by implementing them in terms of the underlying basic_streambuf object. Once you have your own stream buffer, building a basic_ostream class that uses that streambuf will cause all of the standard stream operations on that class (such as <<) to start making the appropriate calls to your streambuf.

如果您想了解更多详情,请参阅标准C ++ IOStreams和语言环境。作为一个无耻的插件,我使用本书中的技术构建了封装套接字的流类连接。虽然我的流中的很多代码不会特别有用,但基本结构应该可以帮助您开始。

If you'd like more details on this, an excellent reference is Standard C++ IOStreams and Locales. As a shameless plug, I have used the techniques from this book to build a stream class that wraps a socket connection. While a lot of the code in my stream won't be particularly useful, the basic structure should help you get started.

希望这有助于!

这篇关于如何覆盖C ++标准库类函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆