Guest User

Untitled

a guest
Sep 27th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.17 KB | None | 0 0
  1. Firstly the C++ Language won't allow pass by value because the copy constructor is private for streams. So why does C++ choose to implement streams this way? Its pretty simple when you understand how operating systems assign file handles to running processes and what the scope of those file handles are(streams eventually work there way back to these primitive constructs). When a process starts executing, the operating system will create a number of file handles(streams) automatically, the std::cout, std::cin, std::cerr, std::clog which are global to the running process...By global I mean the entire process has access to them. The same rules apply for any stream that the process opens. So what does this mean? Well if you think about it, it makes sense to make the stream's copy constructor private. What would happen if it wasn't..Lets try an example using pseudo code.
  2.  
  3. Create file stream fs.
  4. pass fs to function by value.
  5. fs is copied by copy constructor which is public for our example
  6. function does something with copied stream
  7. function exits destroying fs with fs's destructor
  8. ..
  9. Now what happens to our original fs? Isn't it now destroyed since the copied fs freed its resources?
Add Comment
Please, Sign In to add comment