static_cast

This is a discussion on static_cast within the C++ Programming forums, part of the General Programming Boards category; Why isnt this the same: Code: time_t now = time (0); unsigned char *p = (unsigned char*)&now; as: Code: time_t ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    static_cast

    Why isnt this the same:

    Code:
    time_t now = time (0);
    unsigned char *p = (unsigned char*)&now;
    as:

    Code:
    time_t now = time (0);
    unsigned char *p = static_cast<unsigned char*> &now;

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    You are missing the parenthesis around &now
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I ger error:

    error C2440: 'static_cast' : cannot convert from 'time_t *__w64 ' to 'unsigned char *'

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Why are you trying to do that? What are you trying to accomplish?

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    No. You can't do that conversion. Think of static_cast as the explicit request of a cast that the compiler could under the right conditions perform implicitly (there's a few exceptions like casts from void*).

    The compiler cannot, and will not, perform a static_cast between those two types. time_t is defined as some sort of integral type (probably a long integer). You are requesting a conversion between pointer to an integral type and pointer to char.

    You could probably convert from time_t to char with a necessary loss of data, but what you are requesting is a conversion between pointers. There's no such static_cast unless one of the pointers in question is a void*.

    You could do that cast with a reinterpret cast. But the results are probably not what you are trying to do.

    I believe you are trying to store the current time in char[] format. Being that the case, what you probably want is this:

    Code:
    char* currtime = asctime(localtime(time()));
    time() returns a time_t representation of the current system time.
    localtime() returns a tm struct constructed from a time_t object.
    asctime() returns a string representation of the tm struct.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You could do that cast with a reinterpret cast. But the results are probably not what you are trying to do.
    I'd wager it's exactly what he's trying to do, as long as he's trying to do the same thing as the code he got that snippet from. It looks suspiciously like something I wrote that puns the value of a time_t into a sequence of bytes for hashing.
    My best code is written with the delete key.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Well... on that case he will definitely need reinterpret_cast.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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