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:

  1. create subclass of basic_streambuf allows open, write, , possibly read files while specifying unix permissions. streams classes designed details of communicating physical devices disk, networks, , console handled basic_streambuf class , derived classes. if want make own stream class, implementing stream buffer excellent first step.

  2. define own class subclasses basic_ostream , installs custom basic_streambuf. default, basic_ostream supports of standard output routines implementing them in terms of underlying basic_streambuf object. once have own stream buffer, building basic_ostream class uses streambuf cause of standard stream operations on class (such <<) start making appropriate calls streambuf.

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

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -