fread and classes

This is a discussion on fread and classes within the C++ Programming forums, part of the General Programming Boards category; is it OK to read class fields from a file using fread, if Rect is a class is this code ...

  1. #1
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345

    fread and classes

    is it OK to read class fields from a file using fread, if Rect is a class is this code OK:
    Code:
    class Rect {
    int x, y;
    }
    Rect rect;
    fread(&rect, 1, sizeof(Rect),  fp);
    will this code read just x and y or some other stuff? this works well with structs but I don't know about classes.
    thx

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    It may work with structs, but since the default scope for classes is private, you shouldn't really be able to access them. If, however you make x and y public, it may work.
    Just try for yourself, although I can't see why you'd want to do that to a class.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    really a class should "save" itself. Because only a class knows how it is stored. If you just write it out, you're likely to ignore dynamic memory etc. Plus, you don't want to write out any pointers that won't mean anything later.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    the struct/class I'm trying to do doesn't contain pointers, just normal variable types (int char ...)
    I tried it with a class, its fields were public, and it worked, I'll stick to structs though, sounds much safer to me.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    or, like I said you could have a save/restore method. If the class knows that its safe to just flat out write itself or read itself then it should.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    by the way, there's nothing "safer" about structs in C++. They're the same thing with a default of private instead of public.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by FillYourBrain
    by the way, there's nothing "safer" about structs in C++. They're the same thing with a default of private instead of public.
    really, is this the only difference between them? though classes may contain more data or something.
    structs are working well, so I guess it's safe.
    the reason I'm not using save/restore is because I'm reading a huge non-constant number of structs, reading all of them at once is better for performance I thing.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    That is true. But I guess to answer your original question... for a struct/class of that type you can certainly just do a read on the memory. classes too.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    thx a lot then, problem figure out
    or there wasn't actually a problem

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, there is little difference, to the compiler, between a struct and a class, but good programming tends to make a distinction. I use "struct" for my "struct-like" classes -- these have all their data public; their only real purpose is to bundle a collection of data together, and perhaps to do some trivial operations (like a rectangle struct might have length() and width() functions). I also never create "struct-like" classes that contain dynamic memory, or non-primitive types.

    All other classes, though, I would use "class" for, and all data in these classes should ALWAYS be private.

    For my "class" classes, when needed they obviously are able to read and write themselves to disk, as they should be. For "struct-like" classes, I typically just use stream functions read() and write().

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21