As you can see you have instantiated an instance of Foo in your main() function and have called func1() on the Foo instance, which in turn creates an instance of Fee calling f(), and passing a pointer to the calling Foo instance thus allowing the instance of Fee to call a public functions on Foo. I know this looks prety pointless but somtimes you want a class instance to be able to call back into its calling parent and by passing the pointer from 'this' aka self (in Visual Basic parlance) you are then able to do that...
You are passing a pointer from the current instance to your file.write(...) function which is therefore able to get access to public functions, getters and setters. So, you are probably in a document of some sort and wanting to write the document out to a file, by passing a pointer to this the file instance can get access to the data it is to write out to file... plus name of file etc... Quite why you would be forcing this to a char* I don't know without knowing further info
because the write function requires a char* - when writing in binary mode (usually binary) you pass a char* and the size of the thing writing to file (or fstream) - i would suggest only doing this type of write with simple built types - for your own types write your own serialize/deserialize functions or you will have many issues
@majidkamali1370
Is this correct?
file.write( reinterpret_cast<const char*>(this), sizeof(this) );
It is in fact the same as was written above by me.
EDIT: Except as correctly pointed out EssGeEich that instead of sizeof( this ) shall be sizeof( class ) where class is the name of this particular class.
Just to clarify because I don't see it written anywhere above, if you're after the general meaning of the this keyword in a C++ class, it is just a pointer to itself.