Skip to main content

Operators

  • Chapter
  • First Online:
C++ for Lazy Programmers

Abstract

You may have seen this error:

char string1[] = "Hello", string2[] = "Hello"; if (string1 == string2) ...

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 34.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    If I don’t, C++ will, like so:

    String::String () { contents_ = nullptr; };

    String::String (const String& other) { contents_ = other.contents_; }

    So we’ll end up using nullptr, which I’d decided against, and we’ll share memory between Strings so altering one alters the other. A perfect justification for the Golden Rule of Constructors.

  2. 2.

    When discussing a member function, I’ll usually start it with String:: to clarify that it’s a member. We omit String:: when inside the class definition.

  3. 3.

    This whole operator business is syntactic sugar. You could call these functions the ugly way:

    if (stringA.operator==(stringB))...

  4. 4.

    There’s an exception: the ?: operator. Here is an example of its use:

    cout << (x>=0 ? “positive” : “negative”);

    This means that if (x>=0) cout << “positive”; else cout << “negative”;

    I don’t use it much. C++ won’t let you overload it anyway.

  5. 5.

    See Exercise 3 for an interesting tweak on this algorithm.

  6. 6.

    In the programming language LISP and elsewhere, foo is used to name a variable when it’s obvious what foo is. If two such “placeholder” names are needed, it’s often foo and bar. It’s a good bet this is from the military acronym “FUBAR,” meaning roughly “Fouled Up Beyond All Recognition.”

    Some programmers consider foo and bar to be 3vil because they aren’t descriptive, but I think I’d rather read foo than theString or rightHandSide.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2019 Will Briggs

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Briggs, W. (2019). Operators. In: C++ for Lazy Programmers. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5187-4_17

Download citation

Publish with us

Policies and ethics