Originally Posted by
BraneMxm
So I guess it is a good practice always to use const in the parameter when the passed value isnt changed, only used to go through the tree and ect. Can you explain it to me in more detail? When my teacher asks why I used const I want to be able to explain it.
Thank you
There's two changes const makes...
- Enforces that you do not modify the parameter and can only call const functions to it.
- Allows the function to be called by other functions which also use a const type for the parameter.
The second point can be rephrased as... if you don't use const, then you force everyone who calls your method to not use const. Consider the following:
Code:
void printTreeInfo (bTree const * info_me) { // Const paramater because it doesn't modify the tree.
printf ("Num Leaves: %d\n", countLeaves(info_me)); // Only works if countLeaves accepts a const parameter.
printf ("Num Nodes: %d\n", countNodes(info_me));
// &ct...
}
Addendum:
Trees aren't the best example of needing const-correctness, since you lose the const once you use Ptr->left or Ptr->right.