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 23:59

>>21
Magic instance variable
I don't see the appeal of this. I'd probably even prefer like Visual Basic (ugh), which has = and Is.

The way it works in C♯ is as follows: == on reference types tests reference equality by default, but can be overloaded. == on value types generates an error unless the struct overloads ==. There's also the Object.Equals() method, which every object (reference or value type) has. For reference types it tests reference equality; for value types is tests value equality. In both cases it can be overridden.

There's also two ways of making sure you're testing reference equality: cast one of the objects to object or use object.ReferenceEquals, which is a static method.

Oddly string isn't actually a value type, despite being immutable, it just overloads == and != and overrides Equals. Using the operators will probably be more efficient, since both arguments are known to be strings at compile-time.

In conclusion, C# somehow managed to be more screwed up than Sepples. On second thought, though, Sepples has `references' as well as pointers, so:
    A *a = new A(), *b = new A(), &ra = *a, &rb = *b;
    a == b; // probably false (reference equality)
    ra == rb; // value equality (A's operator ==())

which may not be obvious at first.

-------------------------------

I'm not a fan of operator overloading to the extent that Sepples does it, but == and != is common sense (arithmetic operators can help clarify code too). I'm also mostly fine with Haskell's mass of weird operators like f <^(+.+)^> g since they at least only have one meaning, whereas Sepples turns bit shifts into I/O.

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