问题
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.
Now I want to override open()
function of ofstream
class so it will get a parameter to specify the permissions for user access.
回答1:
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.
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:
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 thebasic_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.Define your own class that subclasses
basic_ostream
and installs your custombasic_streambuf
. By default, thebasic_ostream
supports all of the standard output routines by implementing them in terms of the underlyingbasic_streambuf
object. Once you have your own stream buffer, building abasic_ostream
class that uses thatstreambuf
will cause all of the standard stream operations on that class (such as<<
) to start making the appropriate calls to yourstreambuf
.
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.
Hope this helps!
回答2:
This is not answering your question directly as I wouldn't advise overriding 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.
回答3:
#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.
回答4:
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. It would look like this.
来源:https://stackoverflow.com/questions/6565212/how-can-i-override-an-c-standard-library-class-function