Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::string class instance are immutable or not??

Reply
Thread Tools

std::string class instance are immutable or not??

 
 
praveenraj1987@gmail.com
Guest
Posts: n/a
 
      02-05-2009
i have a doubt regarding std::string class..
that whether their instance are immutable or not.. like as we have in
other language

..net framework..
system.string class instance are immutable.

java
java.lang.string class instance are immutable..

same as in C++..
std::string class are immutable or not???

i hope it is not..
as it is typedef of template<class charT, class traits =
char_traits<charT>, class Allocator = allocator<charT> > class
basic_string.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-05-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> i have a doubt regarding std::string class..
> that whether their instance are immutable or not.


Not unless they are declared as const.

--
Ian Collins
 
Reply With Quote
 
 
 
 
praveenraj1987@gmail.com
Guest
Posts: n/a
 
      02-05-2009
Thanx Alot for your response..
actually there was a guy who was not not agreeing to this.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      02-05-2009
On 5 Feb., 22:16, (E-Mail Removed) wrote:
> i have a doubt regarding std::string class..
> that whether their instance are immutable or not.. like as we
> have in other language
>
> .net framework..
> system.string class instance are immutable.


The string class in C# is funny. It is a class (reference type) but
string is immutable and has many overloaded operators so you can use
it like a value type:

string a = foo();
a += bar(); // a points to another immutable string now (C#)

Assembling a string that way is very costly which is why in C# and in
Java you typically have a StringBuilder class which is like a string
but mutable.

In C++ a non-const string is mutable and has value semantics:

string a = foo();
a += bar(); // a will be the same string object (C++)

There's no need for a "StringBuilder" class.

HTH,
SG
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-05-2009
Jeff Schwab wrote:
> Ian Collins wrote:
>> (E-Mail Removed) wrote:
>>> Thanx Alot for your response..
>>> actually there was a guy who was not not agreeing to this.

>>
>> A simple bit of test code would have put him straight!

>
> True in this case, but misleading in general. Suppose he had been
> wondering about C-style string literals, rather than std::string:
>
> int main() {
> char* p = "hello";
> p[1] = 'a';
> }


"/tmp/z.cc", line 2: Warning: String literal converted to char* in
initialization.

--
Ian Collins
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-05-2009
Ian Collins wrote:

> Jeff Schwab wrote:
> > Ian Collins wrote:
> > > (E-Mail Removed) wrote:
> > > > Thanx Alot for your response..
> > > > actually there was a guy who was not not agreeing to this.
> > >
> > > A simple bit of test code would have put him straight!

> >
> > True in this case, but misleading in general. Suppose he had been
> > wondering about C-style string literals, rather than std::string:
> >
> > int main() {
> > char* p = "hello";
> > p[1] = 'a';
> > }

>
> "/tmp/z.cc", line 2: Warning: String literal converted to char* in
> initialization.


While a very useful diagnostic, it is not a required one.



Brian
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-05-2009
Default User wrote:
> Ian Collins wrote:
>
>> Jeff Schwab wrote:
>>> Ian Collins wrote:
>>>> (E-Mail Removed) wrote:
>>>>> Thanx Alot for your response..
>>>>> actually there was a guy who was not not agreeing to this.
>>>> A simple bit of test code would have put him straight!
>>> True in this case, but misleading in general. Suppose he had been
>>> wondering about C-style string literals, rather than std::string:
>>>
>>> int main() {
>>> char* p = "hello";
>>> p[1] = 'a';
>>> }

>> "/tmp/z.cc", line 2: Warning: String literal converted to char* in
>> initialization.

>
> While a very useful diagnostic, it is not a required one.
>

Alas, true. Which is a shame.

--
Ian Collins
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-06-2009
(E-Mail Removed) wrote:
> i have a doubt regarding std::string class..
> that whether their instance are immutable or not.. like as we have in
> other language
>
> ..net framework..
> system.string class instance are immutable.
>
> java
> java.lang.string class instance are immutable..

No such class exists. but the java.lang.String class is indeed
"immutable" (with the exception of hashCode(), but externally it is
immutable).
I believe .NET String is also immutable, but I don't know.
>
> same as in C++..
> std::string class are immutable or not???

std::string is definitely mutable, "const std::string" on the other hand
is not.

>
> i hope it is not..

You're hope is answered.
> as it is typedef of template<class charT, class traits =
> char_traits<charT>, class Allocator = allocator<charT> > class
> basic_string.

What does this have to do with your hope?

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
praveenraj1987@gmail.com
Guest
Posts: n/a
 
      02-06-2009

> You're hope is answered.> as it is typedef of template<class charT, class traits =
> > char_traits<charT>, class Allocator = allocator<charT> > class
> > basic_string.

>
> What does this have to do with your hope?
>


as basic_string are mutable.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      02-06-2009
On 5 Feb., 23:48, Victor Bazarov <(E-Mail Removed)> wrote:
> SG wrote:
> > The string class in C# is funny. It is a class (reference type) but
> > string is immutable and has many overloaded operators so you can use
> > it like a value type:

>
> > * string a = foo();
> > * a += bar(); // a points to another immutable string now (C#)

>
> > Assembling a string that way is very costly which is why in C# and in
> > Java you typically have a StringBuilder class which is like a string
> > but mutable.

>
> > In C++ a non-const string is mutable and has value semantics:

>
> > * string a = foo();
> > * a += bar(); // a will be the same string object (C++)

>
> > There's no need for a "StringBuilder" class.

>
> This information is misleading, I believe. *How is the fact that in C#


Misleading was certnainly not my intention.

> 'a' suddenly "points to another" string different than the fact that in
> C++ 'a' *can* reallocate it buffer?


Yes, there's little difference. But in C# the string OBJECT is a new
one (as in identity) and the object _reference_ has been mutated
whereas in C++ the string _object_ is mutated.

> To the programmer, 'a' is the same in both cases.


Yes.

> So, semantically, C++ string and C# strings are the same, mutable.


If by "C# string" you mean a C# string reference then yes. The C#
string _object_ isn't. But we can say that a C# string object
_reference_ behaves like a "value type" (in .NET terminology). That's
why I said "C# string is funny". I didn't point out explicitly that to
a programmer there's no difference but it was part of what I tried to
communicate.

Cheers!
SG
 
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
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
RV: How to make an immutable instance Batista, Facundo Python 0 06-22-2004 01:06 PM
How to make an immutable instance Batista, Facundo Python 4 06-18-2004 10:36 AM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments