c++ - Function parameters: const matching declaration and definition -


this question has answer here:

found accident function declaration , definition may not agree on constness of parameters. i've found information (links following), question why const matching optional by-value parameters, const matching required reference parameters?

consider following code available here.

class myclass {   int x;   int y;   int z;  public:   void dosomething(int z, int y, const int& x);   int  somethingelse(const int x);   void another(int& x);   void yetanother(const int& z); };  void myclass::dosomething(int z, const int y, const int& x) // const added on 2nd param {   z = z;   y = y;   x = x; }  int myclass::somethingelse(int x) // const removed param {   x = x;   x = 3;   return x; }  void myclass::another(int& x) // const not allowed on param {   x = x; }  void myclass::yetanother(const int& z) // const required on param {   z = z; } 

i've found this on so, looking explanation name mangling. i've found this on so , this on so, don't go detail on why const matching required reference parameters.

when pass value, argument local variable of function. whatever pass copied. if argument const t, means function cannot modify own variable. caller shouldn't know or care that.

passing const t& refers access of variable not belong function. same const t*. not same t* const, 1 mean function cannot modify own pointer. pointer belongs function, if function wants reassign point else that's own business. points not belong function, whether function gets const access or not relevant caller.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -