Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 2 [3]

Author Topic: C++ Program issue  (Read 2991 times)

olemars

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #30 on: March 08, 2011, 04:03:56 am »

I think most C/C++ compilers convert string literals to char[]s (or char*'s, same thing really), making it not an object.
There may be a way to force it though, something like
Code: [Select]
(new string("foo")).equals(var)not sure if that's proper C++,

String literals are constants yes, not objects.
Using std::string correct c++ code would be
Code: [Select]
var == "foo" // order irrelevantor
Code: [Select]
var.compare("foo") == 0or
Code: [Select]
string("foo").compare(var) == 0
As long as var is a string object and not a pointer to a string object it will never be NULL, it will at worst be an empty string.

Quote
I've been focusing on plain C for now.

Why?
Logged

Blank Expression

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #31 on: March 08, 2011, 10:53:24 am »

That's pretty clever, actually...any idea if C++ allows a method to be used on a constant in that fashion?  i.e. "foo".equals(someString)?
No, it doesn't support it; when you use a language intended for developers who still exhibit prokaryotic traits you pass up most of the niceties of modern programming.

You can hack around it by doing things that are in fact harder to read than the standard ways of doing a comparison. They're also less efficient than a standard comparison--the reason it's not a perf hit in C# or Java is because string literals in either language are interned objects that don't have to be created at invocation. C++, being a stupid language (in the literal sense), has no such concept. (EDIT: SolarShado's use of 'new' above is not a good idea--it leads to memory leaks. It should be stack allocated instead.)

The obvious and correct answer is to not use stone knives (C++ or, if you're especially masochistic, C) when you have a Sawzall (tools developed in the last twenty years--pick one).


Quote
I think most C/C++ compilers convert string literals to char[]s (or char*'s, same thing really), making it not an object.
A string literal in C++ is of type const char[], IIRC (or, in other words, const char* and the literal includes a \0 on the end).
« Last Edit: March 08, 2011, 11:09:47 am by Blank Expression »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: C++ Program issue
« Reply #32 on: March 08, 2011, 11:02:37 am »

developers who still exhibit prokaryotic traits
Blacken, is that you again?!
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C++ Program issue
« Reply #33 on: March 08, 2011, 12:08:50 pm »

You can negate the performance hit by storing it in a static variable, so that it only needs to be initialized once. Alternatively a well-named inline function that can properly handle a null could work. Or the simple (var != null && var.compare("foo") == 0).

None of those would be as clean as some other languages, though.
Logged
Eh?
Eh!

kg333

  • Bay Watcher
  • Derp.
    • View Profile
    • Steam Profile
Re: C++ Program issue
« Reply #34 on: March 08, 2011, 06:00:01 pm »

That's pretty clever, actually...any idea if C++ allows a method to be used on a constant in that fashion?  i.e. "foo".equals(someString)?
No, it doesn't support it; when you use a language intended for developers who still exhibit prokaryotic traits you pass up most of the niceties of modern programming.

You can hack around it by doing things that are in fact harder to read than the standard ways of doing a comparison. They're also less efficient than a standard comparison--the reason it's not a perf hit in C# or Java is because string literals in either language are interned objects that don't have to be created at invocation. C++, being a stupid language (in the literal sense), has no such concept. (EDIT: SolarShado's use of 'new' above is not a good idea--it leads to memory leaks. It should be stack allocated instead.)

The obvious and correct answer is to not use stone knives (C++ or, if you're especially masochistic, C) when you have a Sawzall (tools developed in the last twenty years--pick one).

Hehe, I have a friend a lot like you...despises programming in anything less abstract than Java.  Sending him snippets of code involving C/C++ pointers and manually allocated memory is quite amusing.  BTW, kudos for using "prokaryotic", that's one of the more creative insults I've heard thrown my way.  However, real masochists use assembly.   :P

Quote
I've been focusing on plain C for now.

Why?

Not sure why the other guy uses it, but it's still the language of choice for microprocessor programming, unless you feel like going all out hand-optimizing your code in assembly.

KG
Logged

olemars

  • Bay Watcher
    • View Profile
Re: C++ Program issue
« Reply #35 on: March 08, 2011, 06:17:00 pm »

Maybe I should start a FORTRAN thread just to bother programming language crusaders.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: C++ Program issue
« Reply #36 on: March 08, 2011, 06:23:59 pm »

Nah, just jump straight to Brainfuck, Malbolge, and Intercal. Any language that was designed to be productive is too user-friendly to really bother people.
Logged
Eh?
Eh!

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: C++ Program issue
« Reply #37 on: March 08, 2011, 06:32:19 pm »

Nah, just jump straight to Brainfuck, Malbolge, and Intercal. Any language that was designed to be productive is too user-friendly to really bother people.

Hello world in Brainfuck:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Cecilff2

  • Bay Watcher
  • PikaaAAAAあああああ
    • View Profile
Re: C++ Program issue
« Reply #38 on: March 09, 2011, 10:29:14 am »

Logged
There comes a time when you must take off the soft, furry slippers of a boy and put on the shoes of a man.
Unless of course they don't fit properly and your feet blister up like bubble wrap.
Oh ho ho, but don't try to return the shoes, because they won't take them back once you've worn them.
Especially if that fat pig Tony is at the desk.
Pages: 1 2 [3]