Thread: casting

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    casting

    Code:
    ofstream foo;
    foo.write(reinterpret_cast<const char *>(&Object), sizeof(ClassOfObject));
    I've encountered this kind of conversion many many times.

    write() expects a 'const char *' as its first arguemnt. After casting the object , the char pointer will be pointing to our object , right? how does that happen? how can a 'char' pointer manipulate an object of a different type?



    appreciate your help
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Every object is ultimately an array of bytes. This cast accesses those bytes.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Pointer types are fundamentally the same: They all point to an address. The difference between them arises from the compiler's interpretation of what to do with them; for a pointer of type char*, incrementing or subscripting the pointer will move in 1-byte blocks, while pointers of type Object* will move in leaps of (sizeof(Object)) bytes. When you're trying to dereference a pointer, it is the pointer type that tells the compiler what kind of object is expected to be present at that location (although it could really be anything), and the compiler will act accordingly - so (*obj).doSomething() will actually have meaning.

    Accordingly, if you typecast the Object* to char* or const char*, then the Object pointed to will simply be interpreted as an 'array' of char by the compiler, allowing you to manipulate individual bytes at a time. There's some issue with using the signed char* though, something to do with the possibility of a problem that may arise if you try dereferencing it, depending on the internal representation of char (could be +0 or -0 or something), so generally if you are manipulating bytes yourself you should use (unsigned char*) instead. Of course, since the library function uses char*, that's what you should pass to it - and you can safely assume that the library function will handle the binary data correctly.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I'm afraid already know what you mentioned in the first paragraph , i was wondering about how that function deals with the objects since its treating it as a char. I'm guessing that function (like write()) can't go like Object->MemberFucntion(); where Object is a char pointer , right?

    allowing you to manipulate individual bytes at a time.
    So write() simply takes that object and save it byte by byte , right? is this why it always write in binary mode?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I'm afraid already know what you mentioned in the first paragraph
    Sure hope someone else learned something from it then

    how that function deals with the objects since its treating it as a char.
    Well, it deals with the object exactly as if it were an array of char. Since write() wasn't meant to deal with it in any other way, there will be no attempt anyway.

    I'm guessing that function (like write()) can't go like Object->MemberFucntion(); where Object is a char pointer , right?
    No, it can't - for the aforementioned reasons. However, if the char* were casted back to Object* (which would require prior knowledge of the object's type), then yes Object member functions could be called from it.

    So write() simply takes that object and save it byte by byte , right? is this why it always write in binary mode?
    I am not 100% sure about this, but I believe as long as the file is opened in text mode, even if you use write() the '\n' characters will be converted to "\r\n" pairs in the output.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    it could've used templates (but i guess they did it this way for a reason).

    Thanks Hunter2
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM