C++ Revisited
I have learnd C++ before and I know C++.But I cannot write any piece of C++ program without compiler's complaints. Itis the time to re-visit it.
I take it as a new language though I knowsomehting about it, though I am quite familiar with C and Java. It is a newlanguage.
Here is what I get when learning C++ andthey are worth referencing, I think, when learning C++.
- C++ is a new language for you, once you are determined to learn it, though you might know it, though you are familiar with other programming languges like C, C# or Java.
- C++ is a superset of C. But please forget C when learning and using C++. You know, it is a new languages(if Bjarne Stroustrup name it as Z++ or X++, many people won't connect C with it anymore).
- Keep in mind that reference is an aliase to a variable, but not the variable, As a result, whenever you see a reference, you should ask yourself, what and where is the variable? Is there another aliase?
- Reference cannot be applied to literal constants
int &a = 4; // Error
- For object arguments pass-by-reference; for built-in types, pass-by-value.
Pass-by-value would invoke copy constructorand assignment operator which is costly for objects. For built-in types, copyand assign would cost too much and most times, people like passing literalconstants which would cause error if argument is pass-by-value.
void Grade::setScore( const float &sc);
math.setScore( 89.5f ); // Error setScoreuse pass-by-reference.
- Return object as possible as you can instead of returning reference or pointers.
Return reference might cause fatal errors,you might return a reference to local variables.
Returning pointers might cause memory leak.
Return objects is much safer thoughinvoking copy constructor.
- Please read <Effective C++> at least twice before you write formal C++ codes.