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

Re: string vs string* by Scott

Scott
Thu Apr 17 19:55:33 CDT 2008

"Jim Johnson" <aopiyy001@yahoo.com> wrote in message
news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s@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]


Re: string vs string* by Jonathan

Jonathan
Thu Apr 17 21:59:51 CDT 2008

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@yahoo.com> wrote in message
news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s@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


Re: string vs string* by Tom

Tom
Fri Apr 18 11:30:24 CDT 2008

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@yahoo.com> wrote in message
news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s@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


Re: string vs string* by Giovanni

Giovanni
Fri Apr 18 19:00:50 CDT 2008


"Jim Johnson" <aopiyy001@yahoo.com> ha scritto nel messaggio
news:rdmf04pem17orsjrhnlv89kspvfu4d2c9s@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



Re: string vs string* by Hendrik

Hendrik
Mon Apr 28 03:20:47 CDT 2008

Jim Johnson <aopiyy001@yahoo.com> wrote:
> The following line is illegal?
> std::string line = NULL;
> // for a value I cannot set to null as it's not an address

Yes. It initializes a 'std::string' with the 'NULL' pointer.
While the 'NULL' pointer is a valid 'const char*', and you
can initialize 'std::string' with 'const char*', you must
not initialize 'std::string' with 'NULL', as its constructor
doesn't check for 'NULL'.
If you want an empty string, just write
std::string line;

> Then I should write like following?
> std::string * line = NULL; // a pointer I can set to null pointer

This is something completly different. It creates a pointer
that points to address 'NULL'.

Schobi

(F'up)

--
SpamTrap@gmx.de is never read
I'm HSchober at gmx dot de
"I guess at some point idealism meets human nature and
explodes." Daniel Orner