inheritance - How can I override an C++ standard-library class function? -
how can override c++ standard-library class function? in application, use ofstream objects in many different places of code. , want open files in different permission mode in linux, ubuntu. open function of ofstream has no parameter specify permission mode of file creats.
now want override open() function of ofstream class parameter specify permissions user access.
for starters, clarify terminology, stl refers subset of c++ standard library containing containers, iterators, , algorithms. streams classes part of c++ standard library, not bundled stl. purists insist there no such thing stl in c++ standard library (since stl is, technically speaking, third-party library adopted standard), c++ programmers know mean.
as question, there no way within standard specify permission modes ofstream. if want create own custom stream class ofstream supports permissions, best bet following:
create subclass of
basic_streambufallows open, write, , possibly read files while specifying unix permissions. streams classes designed details of communicating physical devices disk, networks, , console handledbasic_streambufclass , derived classes. if want make own stream class, implementing stream buffer excellent first step.define own class subclasses
basic_ostream, installs custombasic_streambuf. default,basic_ostreamsupports of standard output routines implementing them in terms of underlyingbasic_streambufobject. once have own stream buffer, buildingbasic_ostreamclass usesstreambufcause of standard stream operations on class (such<<) start making appropriate callsstreambuf.
if you'd more details on this, excellent reference standard c++ iostreams , locales. shameless plug, have used techniques book build a stream class wraps socket connection. while lot of code in stream won't particularly useful, basic structure should started.
hope helps!
Comments
Post a Comment