Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

noob question c++

Name: Anonymous 2009-07-12 13:15

Why doesn't this work?

#include <iostream>
using namespace std;
int main(int argc,char** argv){
   string name;
   cout<<"What is your name?"<<endl;
   cin>>name;
   if(name=="Anonymous")
   cout<<"Aw don't be shy..."<<endl;
   else
   cout<<"Hello "<<name<<"!"<<endl
   return 0;
}

Name: Anonymous 2009-07-12 21:32

>>17
But just because it's "more common" doesn't mean you can just get rid of reference equality; there are plenty of scenarios in which you do need it. You can't replace == unless you can give me a way of doing reference comparison. I agree that == should be a value comparison for immutable types (afaik C# sort of works this way?), except that now you have to know the details of a type before you can figure out whether == will do a value or reference comparison. This is the bigger problem with these languages like C# and Java which confound value and pointer types.

This is why I believe the C++ syntax works best in this respect. == is always a value comparison; it's just that the type of 'string*' is a pointer, with a pointer value, so comparing string pointers is a reference comparison. You only need to know the types of the variables you're comparing (is it string or string* ?), not the specification or implementation of the types themselves.

The real fix for this imho is that == in Java should always generate a compiler error for immutable types. This simplifies the syntax: == is always a value comparison for *primitives only*; always a reference comparison for everything else; and you are prevented from doing a reference comparison on types for which the result is undefined.

>>18
It makes sense for there to be a need for a compare function that can return >, == or < in the return value. This sort of stops making sense though when you realize that member function type is nonsensical, whereas it should be typed the same as a global function where an additional first argument is the object. Because of these type rules, you can't make a generic function that uses a compare function without templating it.

Lots of little things like this lead to the horrible templating that is STL.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List