|
From: Jim Johnson on 17 Apr 2008 19:16 The following line is illegal? std::string line = NULL; // for a value I cannot set to null as it's not an address Then I should write like following? std::string * line = NULL; // a pointer I can set to null pointer
From: Scott McPhillips [MVP] on 17 Apr 2008 20:55 "Jim Johnson" <aopiyy001(a)yahoo.com> wrote in message news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s(a)4ax.com... > The following line is illegal? > std::string line = NULL; > // for a value I cannot set to null as it's not an address > > Then I should write like following? > std::string * line = NULL; // a pointer I can set to null pointer Why do you want to set something to NULL? In most circumstances you can simply do std::string line; The string is empty, and code that uses it can check to see if it is empty. -- Scott McPhillips [VC++ MVP]
From: Jonathan Wood on 17 Apr 2008 22:59 No. But, as far as I'm concerned, have no good reason to want to set it to NULL. However, if you want to explain what you feel your reason is, I'd be happy to discuss it further. -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com "Jim Johnson" <aopiyy001(a)yahoo.com> wrote in message news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s(a)4ax.com... > The following line is illegal? > std::string line = NULL; > // for a value I cannot set to null as it's not an address > > Then I should write like following? > std::string * line = NULL; // a pointer I can set to null pointer
From: Tom Serface on 18 Apr 2008 12:30 Typically you would either assign an actual value to the string or just check to see if it is empty: std::string line; /// do something if( !line.empty() ) { // do something when line has a string } Tom "Jim Johnson" <aopiyy001(a)yahoo.com> wrote in message news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s(a)4ax.com... > The following line is illegal? > std::string line = NULL; > // for a value I cannot set to null as it's not an address > > Then I should write like following? > std::string * line = NULL; // a pointer I can set to null pointer
From: Giovanni Dicanio on 18 Apr 2008 20:00 "Jim Johnson" <aopiyy001(a)yahoo.com> ha scritto nel messaggio news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s(a)4ax.com... > The following line is illegal? > std::string line = NULL; Yes, it is illegal. "line" is an instance of std::string class, and this instance is allocated on the stack (not on the heap); it is not a pointer, so you can not assign to NULL. The statement: std::string line; is just fine if you want to create an instance of std::string class (on the stack), and set this instance to an empty string (""). > Then I should write like following? > std::string * line = NULL; // a pointer I can set to null pointer This is correct C++, because in this case "line" is a pointer (not a std::string instance created on the stack), so you can assign the pointer to NULL. If you want to share instances of std::string class between different modules or different parts of code, you may use pointers, of course. However, if you can clarify your goal, we may give better answers about that... Giovanni
|
Next
|
Last
Pages: 1 2 Prev: Increasing your traffic and rankings to your website... Next: pointer non-pointer |