473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

why string objects cannot be modified unless they have been initialized?

12
As title.
I mean when I key in like following codes:

Expand|Select|Wrap|Line Numbers
  1. string *c=static_cast<string*> (operator new[](3*sizeof(string)));
  2. *c="test";   //error
  3.  
I now know that string objects cannot be modified unless they have been initialized,
but I want to know the reason.
Is that only string object has this rule? or...?

When I wrote another class which is defined by myself:

Expand|Select|Wrap|Line Numbers
  1. class test{
  2. public:
  3.     test(const char*):_data(0){ cout << _data;};
  4. private:
  5.     int _data;
  6. };
  7. int main() 
  8. {
  9.     test *c=static_cast<test*> (operator new[](3*sizeof(test)));
  10.     c[0]="test";            
  11.     return 0;
  12. }
  13.  // output: 0
  14.  
Which means that test c did initialize, but why string couldn't?
Apr 29 '07 #1
2 1694
AdrianH
1,251 Expert 1GB
As title.
I mean when I key in like following codes:

Expand|Select|Wrap|Line Numbers
  1. string *c=static_cast<string*> (operator new[](3*sizeof(string)));
  2. *c="test";   //error
  3.  
I now know that string objects cannot be modified unless they have been initialized,
but I want to know the reason.
Is that only string object has this rule? or...?

When I wrote another class which is defined by myself:

Expand|Select|Wrap|Line Numbers
  1. class test{
  2. public:
  3.     test(const char*):_data(0){ cout << _data;};
  4. private:
  5.     int _data;
  6. };
  7. int main() 
  8. {
  9.     test *c=static_cast<test*> (operator new[](3*sizeof(test)));
  10.     c[0]="test";            
  11.     return 0;
  12. }
  13.  // output: 0
  14.  
Which means that test c did initialize, but why string couldn't?
It is because you are using new incorrectly. Your first code snippet should read
Expand|Select|Wrap|Line Numbers
  1. string *c=new string();
  2. *c="test";   //error
  3.  
The way you use it, you never call the constructor, when means that the memory for the object is in an indeterminate state and random things will happen.

The reason why the other snippet worked is due to the data it holds.

Where in the world did you get the idea of 3*sizeof(type) from anyway? And where did you get the syntax of how to use operator new from?

You really should look at reading a good C++ book. I found “Thinking in C++” by Bruce Eckel fairly good, though I don’t know where you are starting from, so it may be good for you or it may be over your head, I dunno. It is a free book though, just search for it on the web.


Adrian
Apr 29 '07 #2
huan
12
It is because you are using new incorrectly. Your first code snippet should read
Expand|Select|Wrap|Line Numbers
  1. string *c=new string();
  2. *c="test";   //error
  3.  
The way you use it, you never call the constructor, when means that the memory for the object is in an indeterminate state and random things will happen.

The reason why the other snippet worked is due to the data it holds.

Where in the world did you get the idea of 3*sizeof(type) from anyway? And where did you get the syntax of how to use operator new from?

You really should look at reading a good C++ book. I found “Thinking in C++” by Bruce Eckel fairly good, though I don’t know where you are starting from, so it may be good for you or it may be over your head, I dunno. It is a free book though, just search for it on the web.


Adrian
In fact, I've heard of that book.
I will buy the book some day.
Thanks for your advice! :-)
May 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: rmathieu | last post by:
Hi, I want to initialize a static String array in MC++. What I want to do is to initialize my String array like the C# way: new String {"11", "22"} but I could not find an equivalent in MC++. The...
2
by: Xiaoshen Li | last post by:
Dear All, I hear a lot that cstring cannot be changed. But my code: /************************************************************* * Testing cstring(a pointer) *...
32
by: santosh | last post by:
In following code char str = "asdf" ; str = 's' ; is possible. But char *str = "asdf" ; str = 's' ; is an run-time error. What i understand is *str = "asdf" is stored in Read-only-Memory....
3
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
18
by: greenflame | last post by:
I want to make a function that does the following. I will call it thefunc for short. '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>"
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.