Pages: « 1 2
  Print  
Author Topic: lol streams  (Read 18100 times)
Offline (Male) RetroX
Reply #15 Posted on: March 05, 2010, 09:37:59 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #16 Posted on: March 05, 2010, 09:54:21 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.

"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.

Also, you don't need to use const_cast to get it represented as a const char. :P
Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #17 Posted on: March 05, 2010, 10:04:53 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.

"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.

Also, you don't need to use const_cast to get it represented as a const char. :P
Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
Whenever I have tried to do it without const_cast, G++ bugs me about a depreciated conversion (that works).  I always use -Wall and purge all warnings and errors, and that is one that I happen to get into the habit of doing.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) score_under
Reply #18 Posted on: March 06, 2010, 08:59:16 am

Member
Joined: Aug 2008
Posts: 308

View Profile
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Logged
Offline (Male) RetroX
Reply #19 Posted on: March 06, 2010, 03:25:17 pm

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Exactly.  It's pointless.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) score_under
Reply #20 Posted on: March 06, 2010, 08:30:41 pm

Member
Joined: Aug 2008
Posts: 308

View Profile
That's const char*

char* would mean something like this:
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
or
Code: [Select]
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
Wait, what?
Exactly.  It's pointless.
Tell me...
Code: [Select]
char x[]={'h','e','l','l','o',',',' ','\0'};
char y[]={'d','a','m','n','i','t','\0'};
char *z=x+y;
printf("%c",z[2]);
...What happens when you run that? If you say "prints l" I'll shoot your face off. If you say "access violation because you added 2 random pointers", then I'll forgive you... for now.
Logged
Offline (Unknown gender) Micah
Reply #21 Posted on: March 06, 2010, 09:45:48 pm

Resident Troll
Joined: May 2008
Posts: 128

View Profile
Of course it's adding two random pointers. He was explaining what I meant when I asked if you could overload operator+ for two char *s.
Logged
Offline (Unknown gender) score_under
Reply #22 Posted on: March 06, 2010, 10:15:45 pm

Member
Joined: Aug 2008
Posts: 308

View Profile
I see, though overloading the + for a char* is really pointless IMO.
Logged
Offline (Male) Josh @ Dreamland
Reply #23 Posted on: March 06, 2010, 10:18:28 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
Yeah, compiler presently concatenates "" + "" automatically. For "" + anything else, I want it to cast first "" to variant.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #24 Posted on: March 06, 2010, 11:19:35 pm

Resident Troll
Joined: Feb 2008
Posts: 954
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
It concatenates "" "" automatically. someCharPtr + "" is what might be useful to overload, to avoid std::string constructor noise. However, I agree that it's not very useful. Allowing operator overloading on arbitrary types would make the language completely unpredictable; it'd be worse than monkeypatching in Ruby.
Logged
Pages: « 1 2
  Print