[英]How can I override an C++ standard-library class function?
How can I override a C++ standard-library class function?如何覆盖 C++ 标准库 class function? In my application, I use ofstream
objects in many different places of code.在我的应用程序中,我在许多不同的代码位置使用了ofstream
对象。 And now I want to open files in a different permission mode in Linux, Ubuntu.现在我想在 Linux、Ubuntu 中以不同的权限模式打开文件。 But open
function of ofstream
has no parameter to specify the permission mode of the file it creats.但是open
ofstream
的function没有参数来指定它创建的文件的权限模式。
Now I want to override open()
function of ofstream
class so it will get a parameter to specify the permissions for user access.现在我想覆盖ofstream
class 的open()
function ,因此它将获得一个参数来指定用户访问的权限。
For starters, to clarify your terminology, the STL usually refers to the subset of the C++ standard library containing the containers, iterators, and algorithms.首先,为了澄清您的术语,STL 通常是指包含容器、迭代器和算法的 C++ 标准库的子集。 The streams classes are part of the C++ standard library, but are usually not bundled together with the STL.流类是 C++ 标准库的一部分,但通常不与 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. 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.
As for your question, there is no way within the standard to specify permission modes with ofstream
.至于您的问题,标准内无法使用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:如果您想创建自己的自定义 stream class ,类似于ofstream
但支持权限,您最好的选择是执行以下操作:
Create a subclass of basic_streambuf
that allows you to open, write, and possibly read files while specifying Unix permissions.创建basic_streambuf
的子类,允许您在指定 Unix 权限的同时打开、写入和可能读取文件。 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.流类的设计使得与物理设备(如磁盘、网络和控制台)通信的细节都由basic_streambuf
class 及其派生类处理。 If you want to make your own stream class, implementing a stream buffer would be an excellent first step.如果您想制作自己的 stream class,那么实现 stream 缓冲区将是一个很好的第一步。
Define your own class that subclasses basic_ostream
and installs your custom basic_streambuf
.定义您自己的 class 子类basic_ostream
并安装您的自定义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.默认情况下, basic_ostream
支持所有标准 output 例程,方法是根据底层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
. 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
.
If you'd like more details on this, an excellent reference is Standard C++ IOStreams and Locales .如果您想了解更多详细信息,可以参考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 .作为一个无耻的插件,我使用本书中的技术构建了一个 stream class ,它包裹了一个插座连接。 While a lot of the code in my stream won't be particularly useful, the basic structure should help you get started.虽然我的 stream 中的许多代码不会特别有用,但基本结构应该可以帮助您入门。
Hope this helps!希望这可以帮助!
This is not answering your question directly as I wouldn't advise overriding ofstream::open
.这不是直接回答您的问题,因为我不建议覆盖ofstream::open
。
Instead couldn't you use the first suggestion in this post?相反,您不能使用这篇文章中的第一个建议吗? Open the file as you normally would to get the correct permissions, and then construct an ofstream
from the file descriptor.像往常一样打开文件以获得正确的权限,然后从文件描述符构造一个ofstream
。
#include <iostream>
#include <fstream>
class gstream: public std::ofstream
{
void open(const std::string& filename, ios_base::openmode mode,int stuff)
{
//put stuff here
}
};
int main() {
gstream test;
//io stuff
return 0;
}
seems to work here.似乎在这里工作。
Another option would be to create a wrapper class that contains an 'ofstream' object and has the interface you want, and passes the work onto its 'oftstream' member.另一种选择是创建一个包装器 class ,其中包含一个“ofstream” object 并具有您想要的接口,并将工作传递给它的“oftstream”成员。 It would look like this.它看起来像这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.