[C++]Need help with a customer & salesman question.

Started by
14 comments, last by Trienco 8 years, 2 months ago


* You might want to mark the accessors as const since they don't modify your class contents, but that could start a religious debate on const correctness you may not be prepared to argue.

Make them const. Let's do this.

knights-templar.jpg

(Who actually argues about const correctness? That's like arguing about eating through your mouth.)

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement
Because const correctness quickly becomes an all-or-nothing process in games.

If you start enforcing it in a few places, suddenly you've got functions needing to ensure they are const so they call other functions and those need to be changed. Then it starts to propagate and you've got some library you don't control that operates on a read-only fashion and should be marked as const but isn't, so you need to take steps to fight const correctness because of the third party library.

It quickly becomes an all-or-nothing proposition. If you are providing a library it is less of a choice: be const correct. If you are using libraries and all of them have been careful about that, wonderful for you. But if you re using a bunch of other libraries and any of them are not using it, it quickly becomes the stuff of nightmares.

In practical terms, the main benefit of const correctness is a benefit to the programmer. It can help identify and prevent certain bugs, but assuming other good practices are in place those bugs will be prevented anyway by code tests and code review and QA testing.

Ultimately the options boil down to complete const correctness through the code base or completely avoiding const in the code. In most projects I've worked on professionally, teams have chosen the avoidance route primarily because attempts to reach const correctness are blocked by external libraries.

printf( "Birth Year: %ld\r\n", m_birthYear );

The \r is not required, C++ itself converts platform EOL to/from \n

By leaving it out, code work at all platforms

One thing I seem to be missing, who deletes the customers? Clearing a list of pointers just makes the customers unreachable, they are never freed.

I mean the formatting in the header where there are HUUUGE unstructured gaps on the same line. But that is just my opinion.


That looks reasonable to me too. One tab to indent the constructor from the class body, one tab stop to indent the initializers inside the constructor. The space per tab is a bit more than I would use myself but between the forum software being rather unreliable with formatting and the fact that it will immediately look good as soon as I have it in my own editor with my preferred tab size, I don't see the problem.

Because const correctness quickly becomes an all-or-nothing process in games.

If you start enforcing it in a few places, suddenly you've got functions needing to ensure they are const so they call other functions and those need to be changed. Then it starts to propagate and you've got some library you don't control that operates on a read-only fashion and should be marked as const but isn't, so you need to take steps to fight const correctness because of the third party library.

It quickly becomes an all-or-nothing proposition. If you are providing a library it is less of a choice: be const correct. If you are using libraries and all of them have been careful about that, wonderful for you. But if you re using a bunch of other libraries and any of them are not using it, it quickly becomes the stuff of nightmares.

In practical terms, the main benefit of const correctness is a benefit to the programmer. It can help identify and prevent certain bugs, but assuming other good practices are in place those bugs will be prevented anyway by code tests and code review and QA testing.

Ultimately the options boil down to complete const correctness through the code base or completely avoiding const in the code. In most projects I've worked on professionally, teams have chosen the avoidance route primarily because attempts to reach const correctness are blocked by external libraries.

And now I'm emo. I hope you're happy.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Nobody mentioning the bug in printf (and how it probably shouldn't be used in the first place)?

%ld = long, but the variable is int. This will work great, until you build that code as a 64bit build on Unix, where a long is 64bit and int is 32bit. Most compilers should even warn about this.

Either use %d or (safer) use cout instead of printf.

f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement