operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; I have a vector class and I want to overload some operators for it, so I made the class like ...

"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

I ended up removing the operator=, changing the copy constructor to vec(const vec& rhs), and making the arithmetic operators independent of the class like vec operator(vec,vec) etc. The compiler seems to like this so I'm going to leave it alone on the "if it ain't broke" principle. Thanks for the help.

Shouldn't a constructor use "initialization lists" rather than "assignment"?

It is often technically a requirement if some of the initialisations can throw exceptions (eg initialisation of an instance of a class). For basic types, the argument is more stylistic, particularly if one relies on an optimising compiler.

Originally Posted by Dave_Sinkula

And aren't the semicolons trailing the function definitions superfluous?

Technically, yes. It happens to be a stylistic preference of mine as it makes life a trifle easier if a subseqent decision is made to move the function definition out of the class declaration (which makes the semi-colons in the class declaration no longer superfluous).

And aren't the semicolons trailing the function definitions superfluous?

Technically, yes. It happens to be a stylistic preference of mine as it makes life a trifle easier if a subseqent decision is made to move the function definition out of the class declaration (which makes the semi-colons in the class declaration no longer superfluous).

and all three semicolons in the class declaration are mandatory (or their presence or absence changes meaning of the code, depending on context).

In practice, a fairly common way to factor out functions that are initially declared inline within a class is to cut and paste them out. And I've seen, relatively often, that people forget to add the required semicolons into the class declaration.

Couple this with phenomenon with a large scale program, where the class declaration is in a header file, and such changes necessitate a recompile of several source (eg .cpp) files. If the program is being developed by a team, one common practice is to hold off from major rebuilds of the complete source tree as long as possible (eg overnight for moderate size programs, once a week for large programs). The progress of the whole team can be held up (meaning the team is left waiting with fingers twiddling) if such a major rebuild fails. And one way to make such a rebuild fail is to simply forget to add the semi-colons in the class declaration when refactoring.

Ironically, the common reason for factoring code in such a way is to reduce compile times: forcing multiple source files to compile non-trivial inline functions increases compile times.

Hence my preference for the (technically superfluous) semicolons in the class declaration.