0

My understanding is that using "x" will create a const char[2] (see What is the difference between 'a' and "a"?).

So if one were to write

#include <string>

int main(int argc, char* argv[]) {
  std::string testString("test");
  if (testString == "test") {
    cout << "Success" << endl;
  }
  else {
    cout << "Failure" << endl;
  }
}

then my understanding is that you'd be comparing between types std::string and const char[2].

Is this true, is it good practice, and does it work with all C++ compilers?

I found a useful page on this site, strcmp or string::compare?, which features an answer talking about "overloaded" operators, though even after researching I am still unsure what exactly they are. My educated guess is that the compiler recognises "==" after a string and so converts the const char[2] to a std::string and then uses string::compare.

Community
  • 1
  • 1
LJH
  • 7,444
  • 3
  • 10
  • 19
  • what is your final question? – Arvindsinc2 Mar 30 '17 at 11:33
  • 1
    I think your question is really "what are overloaded operators?". – Oliver Charlesworth Mar 30 '17 at 11:34
  • You should check a [reference](http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp) – NathanOliver Mar 30 '17 at 11:34
  • The standard library, its classes and functions, is part of the C++ specification. If a C++ compiler and standard library doesn't follow the C++ specification then they are not technically C++ compilers or a standard library. I also recommend [this reference wiki](http://en.cppreference.com/w/cpp), where you can find a reference of the [`std::string` comparison operators](http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp). – Some programmer dude Mar 30 '17 at 11:34
  • @LoayAshmawy That is wrong. You can indeed compare things with `std::string` using `operator==`. – Cory Kramer Mar 30 '17 at 11:35
  • I'm starting to think that you don't need the reference site quite yet, but instead [need a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Mar 30 '17 at 11:37
  • Thanks for the help guys! Oliver, you are right. I shall edit the question/do further research in a bit. Knew I shouldn't have posted a question after hours of non stop code writing... – LJH Mar 30 '17 at 11:38
  • 1
    There is actually an overload of `operator==` defined by the standard, that takes `std::string` and `const char *`. – M.M Mar 30 '17 at 11:40
  • I am certainly not fantastic with C++ but it's more just the odd concepts I don't have knowledge of. I could just blindly code using sites like these and common sense but I'm trying to learn the language properly, perhaps a book is a good idea. – LJH Mar 30 '17 at 11:41
  • "I am still unsure what exactly they are. My educated guess is that the compiler recognises "==" after a string and so converts the const char[2] to a std::string and then uses string::compare.", I mean it took me maybe 5 seconds to find [this](http://www.learncpp.com/cpp-tutorial/91-introduction-to-operator-overloading/) . You should take a little time to do some research, you'll get a good and full answer rather than a concise explanation using terms you probably don't yet understand. – George Mar 30 '17 at 11:41
  • Thanks M.M, I an now in the process of finding that in the standard. Sorry for the awful research on my part guys... – LJH Mar 30 '17 at 11:42
  • Thanks for all your help, I have bookmarked the reference site kindly provided by @NathanOliver (and Someprogrammerdude) and I am buying https://www.amazon.co.uk/gp/product/0321714113/ref=ox_sc_act_title_1?ie=UTF8&psc=1&smid=A3P5ROKL5A1OLE and https://www.amazon.co.uk/gp/product/0321334876/ref=ox_sc_act_title_2?ie=UTF8&psc=1&smid=A3P5ROKL5A1OLE . I have used JavaScript for too long and really ought to properly learn C++ before wasting your time. – LJH Mar 30 '17 at 13:03

0 Answers0