So this code is for a command input to be entered in any random order and it will return the value that comes after your input. Amt_Range is a digit checking function.
Why does this work. It should be able to due to the pointer comparison.??
More importantly what does the string() do. By my limited understanding the comparison should not work because a c style string is in the form '-' 't'. Thanks in advance!!
int main(int argc, const char *argv[]) {
string dummy;
int tests = 0, quizzes = 0, assignments = 0, labs = 0, fin = 0;
int testweight = 0, quizweight = 0, assignweight = 0, labweight = 0, finweight = 0;
int counter = 1;
if (argv[counter] == string("-t")) {
dummy = argv[counter + 1];
tests = Amt_Range(dummy, "tests");
counter+=2;
} else if (argv[counter] == string("-q")) {
dummy = argv[counter + 1];
quizzes = Amt_Range(dummy, "quizzes");
counter+=2;
} else if (argv[counter] == string("-a")) {
dummy = argv[counter + 1];
assignments = Amt_Range(dummy, "assignments");
counter+=2;
} else if (argv[counter] == string("-l")) {
dummy = argv[counter + 1];
labs = Amt_Range(dummy, "labs");
counter+=2;
} else if (argv[counter] == string("-f")) {
dummy = argv[counter + 1];
fin = Amt_Range(dummy, "whether there is a final");
counter+=2;
} else {
cout << "wrong input NOW START OVER" << endl;
exit(EXIT_FAILURE);
}
}
The
operator==()
overload that kicks in is a free function in thestd
namespace.It takes the first argument by
const&
which means that a temporary is welcome.A temporary happens to be creatable using the implicit conversion constructor
std::string(char const*)
. So, the overload applies.Now, the reason why the overload
bool std::operator==(std::string const&, std::string const&)
is even found during overload resolution is slightly subtle. This mechanism is known as Argument Dependent Lookup.In this case, ADL works because the second argument is already a
std::string
, which has a type declared in thestd
namespace. Therefore, this namespace is searched for candidateoperator==
overloadsDISCLAIMER:
I've simplified the above. In reality the code in the standard library is much more generic still, and probably looks more like