-
Dec 4th, 2013, 06:53 AM
#1
Thread Starter
PowerPoster
how print a void pointer in string way?
see these code:
Code:
int a=200;
void *b=&a;
how can i print the the *b in string way?
Code:
cout << *static_cast<string*>(b); //gives me an error memory
Code:
cout << *static_cast<char*>(b);//gives me only 1 character(like the tables characters, in ASCII table)
why that 2 exemples give me strange results?
-
Dec 8th, 2013, 01:09 PM
#2
Re: how print a void pointer in string way?
Why would you want a pointer to the string's underlying type? Or why would you do this in the first place? You're likely to receive a result that is not going to be null terminated at any 'proper' point in memory, and there's other issues with this as well.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Dec 8th, 2013, 01:14 PM
#3
Thread Starter
PowerPoster
Re: how print a void pointer in string way?
Originally Posted by AceInfinity
Why would you want a pointer to the string's underlying type? Or why would you do this in the first place? You're likely to receive a result that is not going to be null terminated at any 'proper' point in memory, and there's other issues with this as well.
i want build my own any type. instead the boost::any. and use it without use the static_cast<> or other.
but tell me, please: why i only get 1 character instead the number?
-
Dec 9th, 2013, 01:49 AM
#4
Re: how print a void pointer in string way?
Doing so is much more complex than you think, which is suggestive by how you're demonstrating a conversion between types here. You have to provide the conversion yourself. What number did you use? You know strings are null terminated correct? If you are thinking that this *should* work, then you are not in a position to be creating your own any_type wrapper in my opinion. This is a complex enough and daunting task to achieve with a reasonable level of quality, even for an experienced programmer. Making it portable, fully type safe, etc... I don't see the point. You probably won't end up with a very efficient wrapper in the end, and it would be something ineffective for use 99.9% of the time, and perhaps only used by the lazy programmers out there, even if you did create such a thing. The overhead of having such a wrapper as well--I'm sure most developed programmers would avoid using it as much as possible and just write implementations that they need.
Last edited by AceInfinity; Dec 9th, 2013 at 01:53 AM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|