Quote:Original post by Drew_BentonQuote:Original post by _the_phantom_
... quote ...
Now I know what you are saying, and I have a heck of a lot of books on proper C++ coding stuff (I'm a C++ freak [wink]), but this is all what I have to say. From experience - and I'm talking about a lot of game designing (no games made tohugh [lol])(and I know this is a general forums - thats why our ideas aren't on the same track), the "theorey" of good programming is there to try and help to make some sense of the vastness of C++, but it is just theory - it all changes in practice. Every project is different on its own.
There is truth in what you say - no advice is absolute. That said, avoiding using const saves you little (a few characters of typing) and can cost you much (in debugging - e.g. the infamous if (x = y)).
Quote:Quote:If you need to modify the varible then you should fix your code, not just cast away the const.
If you are given code to work with, and it is a C-style program, and you are adding in C++ concepts, if you dont cast it out you have to rewrite the code - now what is easier and more efficient - rewriting C style code that works to get it to comply with some "good programming practice" or type casting it so it is *compatible* ad works the same.
This is one instance where it does make sense to const_cast away the constness. Provided you're 100% sure the function you're passing to won't try to modify the object. In general though _the_phantom_'s advice is good.
Quote:Im not trying to argue with anyone about the importance or use of const - everybody has their own views and ideas which are valid and totally acceptable and I fully respect them. However I'm just saying in my oopinion - from all my experiences that if you *truly* have data that does not need to be modified - then why are you passing it with all the techniques avalaible from OOP and C++.
I'm not following you here. Why shouldn't you pass it as const?
Quote:Pertaning to your original question - the only reason I could see to pass an argument as a const& would be if it were of some large size - at which case if you have some data type *that* large to where you need to pass a const refrence to it - then your implementations of designs are flawed. What you should be doing is passing it as a const pointer - that is why c++ gave us the -> operator to use.
It doesn't take that much to get an object large enough that passing by reference is more efficient. A couple of members and a base class is usually more than enough. What advantages do const pointers provide over references? I can't think of any. Why should we prefer pointers to references?
Quote:Just my views on the matter. Like I said, I do respect everyones views and opinions here [wink]. I guess it just depends on what you are working with.
P.S. If you were working with a char, which is 1 byte - why would you want to pass it by const refrence (4bytes)? Just an idea. It all goes back to the 3 tradeoffs of programming - speed, size, efficiency - I used to think there must be a way not to ssacrifice 2 of them, but not with the way the programming languages are designed.
(quoted for completeness - I make no comment on this part).
Enigma