C++ fundamentals I still have trouble with

Started by
20 comments, last by rip-off 15 years, 4 months ago
Hmm that does look strange, if the const was on the other side then it would be a const function, or in other words a function that can be run on a const object. You make want to check your compiler because i'm not sure thats right, can someone else confirm? Anyway below is something about const functions in case thats what you meant. Edit: It doesn't compile in visual studio.

Again is about making sure clients use your types properly. You may realise that for built in types passing by reference to const is often more efficient than pass by value. This is to avoid the construction because of the object using in the function. Note that it needs to be const so you are sure that you object won't be modified.

class CMyClass{  //...  void doSomething();  void doSomethingConst() const ;}void SomeFunction( const MyClass &obj ){  obj.doSomething();     //This won't compile, see below.  obj.doSomethingConst();}


Notice that the call to doSomething won't compile. That’s because its not been declared const. The compiler can't be sure that you are invoking a function call that will not modify your object. doSomethingConst is fine, as you have declared it a const function. In other words you are telling the compiler that this function will not modify anything in the object (i.e. member variables ).

In general you should use const quite a lot. When you make new classes you are actually making new types. This means you need to make sure that people use your types properly, or even that you use them properly yourself. If a member
function does not modify any par of the object then it should be declared const, so it can be used by const objects of your type.
Advertisement
The C++ FAQ helped clear things up for me.
Maybe it can help you besides I always recommend all C++ programmers to have a look at it since it seems to answer alot of things you might be wondering about.

FAQs in section [18] Const correctness
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Quote:Original post by zyrolasting
I came across a function that had const after the function name and return type, but before the parameter list.

float foo const ();

Does not compile with g++, maybe you meant float const foo ();?
Quote:Original post by Zahlman
Quote:Original post by Gage64
I'm not sure I agree.

In general, you mark a parameter as const when you don't want it to be changed inside a function. Now, it's true that if the parameter was passed by value, changing it would not affect the original variable, but if the function should not change the parameter, it's still a logical error. Marking the parameter as const allows the compiler to catch these errors.


This is a more reasonable objection, but I'm not convinced that it would catch very much. For that matter, the decision that a passed-by-value parameter shouldn't be changed strikes me as kind of arbitrary - if it doesn't matter for correctness (and since we're making a copy, how can it?), it's just tying the implementor's hands.


I think it can matter for correctness, but I can't think of a good example so here's a bad one:

void print(int arr[], int size) {    size = 13;    // Error    for (int i = 0; i < size; ++i)        cout << arr << endl;}


This function should only print the array so it should not touch the size parameter. If it was marked with const the compiler would have caught this.
Quote:Original post by Gage64
I think it can matter for correctness, but I can't think of a good example so here's a bad one:

void print(int arr[], int size) {    size = 13;    // Error    for (int i = 0; i < size; ++i)        cout << arr << endl;}


This function should only print the array so it should not touch the size parameter. If it was marked with const the compiler would have caught this.

On the other hand, this is a perfectly legitimate method of reversing the same operation:
void print_reverse(int arr[], int size) {    for (--size; size >= 0; --size)        cout << arr[size] << endl;}

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by Gage64
I think it can matter for correctness, but I can't think of a good example so here's a bad one:

...

This function should only print the array so it should not touch the size parameter. If it was marked with const the compiler would have caught this.

On the other hand, this is a perfectly legitimate method of reversing the same operation:

...


Well yeah, but the point was that the function I wrote shouldn't touch size and so it should be const. Your function does change size, so obviously it shouldn't be const.

Again, I know it was a bad example. I just think that if you have a parameter that shouldn't change inside the function, making it const can prevent certain logical errors. If later you decide that you do want to change the parameter, you can remove the const modifier.
Quote:Original post by Gage64
Again, I know it was a bad example. I just think that if you have a parameter that shouldn't change inside the function, making it const can prevent certain logical errors. If later you decide that you do want to change the parameter, you can remove the const modifier.
Forcing a recompile of client code - I don't think an implementation detail like this should be exposed as part of the function's public interface.

This might be a candidate for a pragma (similar to #pragma unused), or just assign the parameter to a const local variable (which will be optimised away), and use that variable instead.

Edit: we seem to have strayed way off topic for the 'for beginners' forum [smile]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thanks guys, I think I get it. I knew what const meant but I was just hazy about any context change when const is put in a less obvious location. I think I'll be good with this now. (Thanks for the link, davi!)

I have a new question in a similar area. I made a static class member. (I understand this is in class scope, not object.) Even so, when I try to compile any build with a static member, I get an unresolved external error. Why is that? I made sure I had a constructor initialize it, and I keep a practice of never altering the value through an object instance.
Quote:Original post by swiftcoder
...


Good points and I agree.
Quote:Original post by zyrolasting
I made a static class member. (I understand this is in class scope, not object.) Even so, when I try to compile any build with a static member, I get an unresolved external error. Why is that?


Clicky.

This topic is closed to new replies.

Advertisement