|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Dear All,
I have a probably simple question but annoying me a lot. For example: std::stringstream ss; int * p = new int(1); ss << p; // Output the address void * q; ss >> q; // I want to input the address, but give a compilation error. if (q != NULL) ... Any way to read an address and see if it is valid? Thanks a lot! Shuisheng |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
shuisheng wrote:
> std::stringstream ss; > int * p = new int(1); > ss << p; // Output the address > void * q; > ss >> q; // I want to input the address, but give a compilation error. > if (q != NULL) > ... What's the error? The following complete program compiles fine with GCC 4.1.2, VC7 and Comeau 4.3.10.1 Beta (online). #include <sstream> #include <iostream> int main() { std::stringstream ss; int * p = new int(1); ss << p; void * q; ss >> q; std::cout << q << "\n"; } But what are you trying to accomplish, anyway? Why don't you put the int in the stream rather than its address? -- Christian Hackl |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> shuisheng wrote: > > std::stringstream ss; > > int * p = new int(1); > > ss << p; // Output the address > > void * q; > > ss >> q; // I want to input the address, but give a compilation error. > > if (q != NULL) > > ... > > What's the error? The following complete program compiles fine with GCC > 4.1.2, VC7 and Comeau 4.3.10.1 Beta (online). > > #include <sstream> > #include <iostream> > > int main() > { > std::stringstream ss; > int * p = new int(1); > ss << p; > void * q; > ss >> q; > std::cout << q << "\n"; > > } > > But what are you trying to accomplish, anyway? Why don't you put the int > in the stream rather than its address? > > -- > Christian Hackl You are right. I have a typo in my testing. I used "void q" rather than "void* q". What I am doing is coding a smart pointer. To output it, I output address and value. In reading, if the address is NULL, it means the pointer does not point a value. Thank you so much! |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
shuisheng wrote:
> On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.at> wrote: >> But what are you trying to accomplish, anyway? Why don't you put the int >> in the stream rather than its address? > > What I am doing is coding a smart pointer. Is this some kind of exercise? If not, you should have a look at the smart pointer classes offered by Boost: http://www.boost.org/doc/libs/1_35_0.../smart_ptr.htm -- Christian Hackl |
|
![]() |
| Outils de la discussion | |
|
|