Cast operation or string conversion?

Discussion in 'C++' started by Giuseppe:G:, Jul 9, 2008.

  1. Giuseppe:G:

    Giuseppe:G: Guest

    Hi,

    I have a function defined as follows:

    bool observe(const WordID* dataword);
    bool observe(const Word* dataword);

    where Word and WordID are respectively

    typedef std::string Word;
    typedef uint32_t WordID;

    My purpose is to use this function with some different data I already
    have: the problem is, this data is NOT in the WordID/Word format.

    What I have is *pairs* of values, that I would like to input together
    into observe(). In other words, I have pairs of numbers whose type is size_t

    (size_t, size_t), (size_t, size_t), ... etc.

    How can I input one of these pairs into observe()? I was thinking of
    creating a pair like in

    X = make_pair(a,b)

    and then use some kind of cast in order to have

    WordID* dataword = dynamic_cast<WordID*>(X)

    But I'm not really sure if what I'm saying sounds reasonable or whether
    there are better ways to achieve this?Such as chaining the two size_t
    into a string and returning a pointer maybe?

    Thanks!

    Giuseppe
     
    Giuseppe:G:, Jul 9, 2008
    #1
    1. Advertising

  2. Giuseppe:G:

    Guest

    On Jul 9, 11:24 am, Giuseppe:G: <> wrote:

    > bool observe(const WordID* dataword);
    > bool observe(const Word* dataword);
    >
    > where Word and WordID are respectively
    >
    > typedef std::string Word;
    > typedef uint32_t WordID;
    >
    > My purpose is to use this function with some different data I already
    > have: the problem is, this data is NOT in the WordID/Word format.
    >
    > What I have is *pairs* of values, that I would like to input together
    > into observe(). In other words, I have pairs of numbers whose type is size_t
    >
    > (size_t, size_t), (size_t, size_t), ... etc.


    Would a unique conversion to Word work? Without knowing whether
    observe() cares about the format of Word, it's difficult to say, but
    this might work:

    So if you have (0x123, 0x42), create a Word that contains the string
    representation of the two value (assuming that size_t is 32 bits):

    "0000012300000042"

    > How can I input one of these pairs into observe()? I was thinking of
    > creating a pair like in
    >
    > X = make_pair(a,b)
    >
    > and then use some kind of cast in order to have
    >
    > WordID* dataword = dynamic_cast<WordID*>(X)


    You can't use dynamic_cast, it's for dynamic class hierarchies with
    virtual functions.

    What you are saying is using the address of the pair as WordID. Yes,
    it can work too but you must take care of the lifetimes of the pairs.
    Insert them in a vector that lives long enough, perhaps?

    > But I'm not really sure if what I'm saying sounds reasonable or whether
    > there are better ways to achieve this?Such as chaining the two size_t
    > into a string and returning a pointer maybe?


    It sounds like either can work.

    Ali
     
    , Jul 9, 2008
    #2
    1. Advertising

  3. Giuseppe:G:

    Guest

    On Jul 9, 11:45 am, wrote:
    > On Jul 9, 11:24 am, Giuseppe:G: <> wrote:


    > > X = make_pair(a,b)

    >
    > > and then use some kind of cast in order to have

    >
    > > WordID* dataword = dynamic_cast<WordID*>(X)


    I forgot to say what would work:

    WordID* dataword = reinterpret_cast<WordID*>(&X)

    Ali
     
    , Jul 9, 2008
    #3
  4. Giuseppe:G:

    Giuseppe:G: Guest

    Hi Victor, thanks. Please find my replies below.

    Victor Bazarov wrote:
    > A pair of size_t objects is *most likely* bigger than an unsigned int.
    > You cannot dynamic_cast anything except pointers to polymorphic classes.
    > That means the approach you're describing is not going to work.
    >
    > I would say convert your tuple into a string using hexadecimal or some
    > other notation and pass the pointer to that string to your 'observe'
    > function.


    Would you be so kind to show me a practical example on how I achieve this?

    >
    > BTW, what's the reason you're passing pointers and not references?
    >


    Well, to be honest observe() it's not my function, I just need to
    integrate it in my code. After looking at it I've seen that the only
    thing it does is incrementing some global count value leaving the
    dataword untouched.

    > V



    Thanks
    Giuseppe
     
    Giuseppe:G:, Jul 9, 2008
    #4
  5. Giuseppe:G:

    Giuseppe:G: Guest

    Hi Ali, thanks very much. Below are some further questions.

    wrote:
    > Would a unique conversion to Word work? Without knowing whether
    > observe() cares about the format of Word, it's difficult to say, but
    > this might work:
    >
    > So if you have (0x123, 0x42), create a Word that contains the string
    > representation of the two value (assuming that size_t is 32 bits):
    >
    > "0000012300000042"
    >

    Well I guess it would because, observe basically just increments some
    global value and I don't think it parses dataword to get content
    information.
    I'm not familiar with C++, would you be so kind to show me some way to
    pratically do this?


    Thanks Giuseppe
     
    Giuseppe:G:, Jul 9, 2008
    #5
  6. Giuseppe:G:

    Dijkstra Guest

    On 9 jul, 21:42, Giuseppe:G: <> wrote:

    > Well, to be honest observe() it's not my function, I just need to
    > integrate it in my code. After looking at it I've seen that the only
    > thing it does is incrementing some global count value leaving the
    > dataword untouched.


    So, if you're pretty sure it does not touch the dataword and only do
    some counting, why don't you pass NULL?

    observe( (const WordID *)NULL );

    If you "observe" by the way some crashing or core dumps you can pretty
    sure the "observe" function touched the WordID. :)

    Cheers,
    Dijkstra.
     
    Dijkstra, Jul 10, 2008
    #6
  7. Giuseppe:G:

    Giuseppe:G: Guest

    Dijkstra wrote:
    > So, if you're pretty sure it does not touch the dataword and only do
    > some counting, why don't you pass NULL?
    >
    > observe( (const WordID *)NULL );
    >
    > If you "observe" by the way some crashing or core dumps you can pretty
    > sure the "observe" function touched the WordID. :)
    >
    > Cheers,
    > Dijkstra.


    hehe yeah I'm observing everything working thanks :D
     
    Giuseppe:G:, Jul 10, 2008
    #7
  8. Giuseppe:G:

    Giuseppe:G: Guest

    Victor Bazarov wrote:
    > A pair of size_t objects is *most likely* bigger than an unsigned int.


    HI, Victor, I studied better my code and realised that I can use that
    observe function only by passing the uint32 pointer to it (the *string
    version does other things). So I guess now I have a more serious
    problem, that of uniquely mapping this pair of (size_t size_t) to and
    uint32. What do you propose to do?

    Is it possible to cast

    An idea is to go redefine the observe function using uint64_t instead of
    uint32

    observe(uint64_t* wordid);

    and then convert each size_t into a uint32 and chain them into a uint64.
    Does it sound reasonable?Something like this

    uint64_t chain(uint32_t one, uint32_t two) {
    // get two 32bit rands and concatenate
    uint64_t longrand = one;
    longrand <<= 32;
    longrand |= two;
    return longrand;
    }

    but I have no clue on how to get a uint32 out of a size_t and if it's
    feasible.
    Can you suggest other ways?

    Thanks so much again

    Giuseppe


    > You cannot dynamic_cast anything except pointers to polymorphic classes.
    > That means the approach you're describing is not going to work.
    >
    > I would say convert your tuple into a string using hexadecimal or some
    > other notation and pass the pointer to that string to your 'observe'
    > function.
    >
    > BTW, what's the reason you're passing pointers and not references?
    >
    > V
     
    Giuseppe:G:, Jul 12, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,117
    Dan Pop
    Feb 10, 2004
  2. david ullua
    Replies:
    13
    Views:
    694
  3. raan
    Replies:
    2
    Views:
    471
  4. Buzz Lightyear
    Replies:
    10
    Views:
    1,162
    Alexander Bartolich
    Aug 12, 2009
  5. William Lopes
    Replies:
    21
    Views:
    738
    Eric Sosman
    Oct 17, 2012
Loading...

Share This Page