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.
Buying options
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsNotes
- 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.
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.
This whole operator business is syntactic sugar. You could call these functions the ugly way:
if (stringA.operator==(stringB))...
- 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.
See Exercise 3 for an interesting tweak on this algorithm.
- 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
Rights and permissions
Copyright information
© 2019 Will Briggs
About this chapter
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
DOI: https://doi.org/10.1007/978-1-4842-5187-4_17
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-5186-7
Online ISBN: 978-1-4842-5187-4
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books