[ alfa-pro @ 12.09.2015. 03:05 ] @
Drustvo zanimam me kada i zasto koristiti constantne parametre i reference u parametrima metoda.
Znam sta su reference i zanam sta su konstante ali nije mi jasno sta bi to moglo da izmeni moj parametar neke metode tako da bi ja morao da ga proglasavam konstantnim?


npr :

Code:

int test(const int& x, const double& y) const {
   ///...
}


Kad je preporucljivo a kada ne. Neki primer?

Hvala
[ X Files @ 12.09.2015. 10:14 ] @
Citat:
Znam sta su reference i zanam sta su konstante ali nije mi jasno sta bi to moglo da izmeni moj parametar neke metode tako da bi ja morao da ga proglasavam konstantnim?


Da, ovo sam se i ja nekada pitao, kada sam radio kao "one man band", kada je ceo kod bio moj. Čim sam upao u prvi tim, objasnili su mi da jedan čovek projektuje klase (deklaracije), drugi razvija metode (definicije) koje je ovaj prvi deklarisao. Treći dođe pa nešto po svemu tome nešto pokušava... U suštini, onaj prvi kaže, imam razlog, ovo nemojte da mi menjate, znam zašto. Ako pokušate, kompajler će vas upozoriti.

Sve u svemu, čistija dokumentacija.

Na primer:
http://www.cprogramming.com/tutorial/const_correctness.html
Citat:

The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).

But why do you care?

Const gives you the ability to document your program more clearly and actually enforce that documentation. By enforcing your documentation, the const keyword provides guarantees to your users that allow you to make performance optimizations without the threat of damaging their data. For instance, const references allow you to specify that the data referred to won't be changed; this means that you can use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. Even if it does, the compiler will prevent the code from compiling and alert you to the problem. On the other hand, if you didn't use const references, you'd have no easy way to ensure that your data wasn't modified.


Što se tiče referenci, dosta toga je poznato /stil, nula, preklapanje, .../:

http://www.open-std.org/jtc1/s...21/docs/papers/2012/n3445.html
http://stackoverflow.com/quest...nst-reference-in-function-args

[ alfa-pro @ 12.09.2015. 17:59 ] @
Znaci ako ja radim sam i nema niko pristup mom kodu onda mogu to i izbeci. Ali u svakom slucaju je bolja praksa navikavati se na vreme... Hvala druze na odgovoru