c++ - When to use const before function and when you shouldn't -


I can not understand that I'm trying to recreate the "complex" C ++ class and those tasks.

For example: using real (), image (), eb () parameters etc. const complex & amp; Obj . So I thought that if you pass an object from your reference then it is not copied and saves a little memory and it uses further const to prevent that object from being messed up is. Am i right So I thought I could make my works as well, but why do const need those functions with parameters with const ? Here's what I found:

  class MyComplex {Private: double real; Double image; Public: My Complex (); My Complex (Int real); MyComplex (integer, real, integer); Double met real (); Double getImag (); Zero set real (int real); Zero set imag (int image); Friend's cut double real (Costmemplex & OBJ); Buddy's Constant Double Image (Constmangplux and OBJ); Friend's Cunt Double Abs (Kont Micromax & OBJ); Friend Kant Double ARG (Cut Microplocks & OBJ); Friend Const Double Harm (Kont Micromax & OBJ); Friend const std :: string conj (const MyComplex & obj); Friend std :: string myPolar (double road, double theta); };  

If I do not put const in front of them then they do not work.

  const double real (const MyComplex & obj) {back obj .real; }  

Other functions are very similar.

announcement

  friend const double real (const MyComplex & obj) ;  

does not declare a const function, it announces a non-conasible function that gives constant value. Since you return anyway, this is the const plain useless.

A single const type is declared as:

  // double getReal () definition within class;  

This creates the underlying parameters for getReal () , this indicator, const , which gives you Permission is to do this myConstComplex.getReal () .

And, as the Deduplicator notes correctly, just litter those friends, you do not need them and you do not want them.


Comments