Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cast operation or string conversion?

Reply
Thread Tools

Cast operation or string conversion?

 
 
Giuseppe:G:
Guest
Posts: n/a
 
      07-09-2008
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







 
Reply With Quote
 
 
 
 
acehreli@gmail.com
Guest
Posts: n/a
 
      07-09-2008
On Jul 9, 11:24 am, Giuseppe:G: <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
acehreli@gmail.com
Guest
Posts: n/a
 
      07-09-2008
On Jul 9, 11:45*am, (E-Mail Removed) wrote:
> On Jul 9, 11:24 am, Giuseppe:G: <(E-Mail Removed)> 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
 
Reply With Quote
 
Giuseppe:G:
Guest
Posts: n/a
 
      07-09-2008
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
 
Reply With Quote
 
Giuseppe:G:
Guest
Posts: n/a
 
      07-09-2008
Hi Ali, thanks very much. Below are some further questions.

http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Dijkstra
Guest
Posts: n/a
 
      07-09-2008
On 9 jul, 21:42, Giuseppe:G: <(E-Mail Removed)> 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.
 
Reply With Quote
 
Giuseppe:G:
Guest
Posts: n/a
 
      07-10-2008
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
 
Reply With Quote
 
Giuseppe:G:
Guest
Posts: n/a
 
      07-12-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments