This pointer

How do i pass this pointer to an fstream read/write function?
I have two possible formats
f.read((char*)&this, sizeof(egclass));
f.read((char*)this, size(egclass));
or is it something completely different?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <type_traits>

struct A
{
    // ...

    std::istream& read_as_bits( std::istream& stm )
    {
        static_assert( std::is_trivially_copyable<A>::value, "class is not TriviallyCopyable" ) ;
        return stm.read( reinterpret_cast<char*>(this), sizeof(*this) ) ;
    }

    // ...
};


However:
When you feel the urge to design a complex binary file format, or a complex binary application protocol, it is generally wise to lie down until the feeling passes.
- ESR in 'The Art of Unix Programming', Chapter 5 'The Importance of Being Textual' http://www.faqs.org/docs/artu/ch05s01.html
Topic archived. No new replies allowed.