6

I was looking at one of the implementation of String class and noticed the following overloaded == operator.

String f = "something";
String g = "somethingelse";
if (f == g)
    cout << "Strings are equal." << endl;

bool operator==(String sString)
{
    return strcmp(operator const char*(), (const char*)sString) == 0; 
}

I understood most of the part except operator const char*() what exactly its been used for? I have very basic knowledge of operator overloading , can someone please throw some more light on this?

jfly
  • 7,715
  • 3
  • 35
  • 65
zer0Id0l
  • 1,374
  • 4
  • 22
  • 36

4 Answers4

11

It is an explicit call to the operator const char*() member function. This code would do the same:

return strcmp(static_cast<const char*>(*this), (const char*)sString) == 0;

But there are more than one thing wrong with that code:

  1. it should not use C-cast, but C++-casts (e.g. static_cast) for the right argument
  2. operator== should be a free function, not a member function
  3. A string class should normally not have an operator const char*
  4. If the String class is implemented reasonably, the operator== should take both parameters as const references
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • 2
    +1 For anyone that wonders about the warrant for #3, [see this question](http://stackoverflow.com/questions/4096210/why-does-stdstring-not-provide-a-conversion-to-const-char) – WhozCraig Sep 09 '13 at 08:12
  • Also, using two different means of achieving the same thing. Whatever means chosen for the first argument (and explicitly calling `operator char const*()` is probably the least intuitive and idiomatic) should be used for the second argument as well. (As to whether `static_cast` or a C style cast, this is largely a question of style. In this case, anyway.) – James Kanze Sep 09 '13 at 08:22
  • @James of course it is a question of style. As is the choice of explicit operator calling ;) – Arne Mertz Sep 09 '13 at 09:11
  • All styles are equal. But some are more equal than others:-). I've never seen code which used the explicit call of a conversion operator; in fact, the explicit call of an operator in general seems limited to forwarding functions (e.g. the postfix `operator++( int )` might call `operator++()`.) There are also conventions where there is a strong underlying technical argument: casts between pointer types should always use C++ casts, for example. On the other hand, _most_ programmers I've seen prefer `(double)someInt` to `static_cast( someInt )`. – James Kanze Sep 09 '13 at 09:19
  • What's wrong with `operator==` being a member function besides lack of commutative property? – SomeWittyUsername Sep 09 '13 at 16:38
  • 1
    @icepack that way the first argument *has* to be a `String` and can't be anything convertible into a `String`. e.g. `String foo; if("something" == foo)` would not compile. If `op==` is a free function, the first parameter can be converted like any normal function parameter. – Arne Mertz Sep 10 '13 at 05:16
  • @ArneMertz That's exactly what the lack of commutativity means. I prefer to implement whatever possible as member and complete the missing functionality by external free implementation (which will be very simple here - just call the member operator with operands order reversed). That's more aligned with OO principles, IMHO. – SomeWittyUsername Sep 10 '13 at 07:04
5

operator const char*() is the old-style C casting: just like you can cast an integer to float by (float)int_var, you can cast to const char* as (const char*)string_var. Here it cast a String to const char *

If you're familiar with the STL std::string, then this operator const char*() is doing basically the same job as .c_str() there.

jfly
  • 7,715
  • 3
  • 35
  • 65
1

This is an explicit call to the cast-to-const char* operator which is overloaded by your String implementation.

Jaffa
  • 12,442
  • 4
  • 49
  • 101
0

This operator will cast it's own String to const char* and call strcmp.

bkausbk
  • 2,740
  • 1
  • 36
  • 52