Which of these is a reasonable ctor for a rational number class?
Rat(int a, int b) { }
Rat(int a_, int b_) { a = a_; b = b_; }
Rat Rat(int a_arg, int b_arg) : a(a_arg), b(b_arg) { }
void Rat(int a, int b) { }
void Rat(int aVal, int bVal) { (a,b) = (aVal,bVal); }
int main(int argc, char *argv[]) { constexpr int num_args = argc; }
argc
will be “captured”, and constant from then on.
argc
is an argument to the function.
argc
isn’t known until run-time.
const constexpr numargs = argc
would be ok.
Is the compiler allowed to assume
that the if
statement will always be true?
void foo(const string &name) { string me = name; bar(); if (me == name) cout << "equal\n"; }
Which of these is the best way to make a+b
work,
where a
and b
are class Foo
?
Foo &operator+(Foo &);
Foo &operator+(Foo);
Foo &operator+(Foo, Foo);
Foo operator+(const Foo &) const;
Foo operator+(const Foo &, const Foo &) const;