So if I create 2 TCP sockets, but don't bind them to a port, and compare them, they are equal. But as
soon as I bind one to port X, they are no longer equal.
If I open() "foo.txt" read only twice, they are equal. As soon as I read one character using one of
the file descriptors (but not the other), they are not equal? (Since your quoted man page includes
the file offset as part of the file description).
If I open() "foo.txt" twice, once as read-only and once as, say, read-write, they are not equal (again,
see man page).
1 2 3 4 5 6 7
|
// Assuming some syntax here
FileDescriptor f1( "foo.txt" );
FileDescriptor f2( "foo.txt" );
FileDescriptor f3( f2 );
// I suppose you are saying now that:
// f1 != f2, but f2 == f3, since f1 and f2 point to different file descriptions whereas f2 and f3 point to the same.
|
If those are the semantics you want, I think the answer to your question is no, you can't know that, because
the file descriptor table is not exposed to you (it is held by the kernel).
But if this is the problem you are trying to solve:
you cannot have e.g. a std::vector of FileDescriptors because when you add an element to the vector, a copy is made and the original FileDescriptor is likely going to be destructed, and thus close() will be called on the fd.
|
That problem should be solved via a "smart fd" class. I suggested it not only because it is the right solution,
but also because I have an implementation of one (it is less than 100 lines of actual C++ code and mimics
the APIs provided by the boost smart_ptr library), but I cannot post it here.