Skip to main content
gamedev.net
🔒 Locked

Const

Started by OleKaiwalker Feb 28, 2004 at 6:40 AM 15 replies 700+ views
Original Post
OleKaiwalker
OleKaiwalker
Hey, Just wondering, what effect does it have, when const is written after the function name? Like this operator const char *() const unsigned int GetLength() const { return something; }
ChaosPhoenix
ChaosPhoenix
It protects the invoking object that is calling that function.
OleKaiwalker
OleKaiwalker
Meaning, that the member data of the class can''t be altered?
Kaijin
Kaijin
Yeah, it''s basically saying that this member function promises not to alter any of data in the object. If it does, the compiler complains.

The point of it is that when you have a const object you can only call the const methods on it.


struct A {
void f() const {}
void g() {}
}

int main() {
const A a;
a.f(); // Fine

a.g(); //! Error

}


You can have both and const and non-const versions of member functions too. The const version is only used when the object is const.


struct A {
void f() const {}
void f() {}
}

int main() {
A a1;
a.f(); // Non-const version called

const A a2;
a.f(); // Const version called

}


You can also mark data members as mutable which means that it can be modified even inside const member functions. This is useful when you need to make changes internally but to the user of the class, externally it hasn''t changed.



- Kaijin
nonpop
nonpop
A bit off-topic, but how can the following compile? There's only one warning of a non-referenced variable ca . The compiler is VC6

#include <iostream>
using namespace std;

class a
{
public:
int count () const
{
int i;
for (i = 0; i < 10; i++)
{
cout << i << endl;
}
return (i);
}
};

int main ()
{
a ca;
cout << "ca.count() = " << ca.count << endl;
return (0);
}


There are no parenthesis after ca.count so is this something ms specific or is it a bug?

I couldn't find anything in the docs.

EDIT: The program output is: 1

EDIT: It works with pointers, too; change a ca; to a* ca = new a (); and ca.count to ca->count (and add delete ca; , of course)

[edited by - nonpop on February 28, 2004 9:50:10 AM]


[edited by - nonpop on February 28, 2004 9:53:59 AM]
Kaijin
Kaijin
quote:
Original post by Anonymous Poster
That''s valid C++. Nowhere in your code your altering member variables in const method.



When he said a bit off topic he actually meant quite a lot This probably would have been better off in it''s own thread.

To answer your question, when you access a function without the the parenthesis you get the address of the functon. You would normally store this in a function pointer or in this case a member function pointer. This can then be used to call the function at a later stage. It''s commonly used for callbacks.

- Kaijin
nonpop
nonpop
But why is it returning 1 ? I think that''s not a very valid address...
Polymorphic OOP
Polymorphic OOP
quote:
Original post by Kaijin
To answer your question, when you access a function without the the parenthesis you get the address of the functon. You would normally store this in a function pointer or in this case a member function pointer. This can then be used to call the function at a later stage. It''s commonly used for callbacks.

- Kaijin



No, actually you are wrong. It would be a member function pointer if he did

&a::count

His expression, a.count without parenthesis is completely illegal C++ code and should generate a compiler error. It''s the fault of his compiler.
Kaijin
Kaijin
I''m not 100% sure about this because I don''t use them but I think member function pointers are a relative offset within a class. When you call them you always have to provide an object to call them on like so:

(object.*func_ptr)(10);

So the value is used to determine which member function to call

- Kaijin
neoztar
neoztar
I ran the code in the debugger and it seems that the function pointer gets implicitly converted to a bool, which results in a '1' being written. To see what's going on, try changing the line to:
cout << boolalpha << "ca.count() = " << ca.count << endl; 


The output is:
ca.count() = true  


~nz 

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //

[edited by - neoztar on February 28, 2004 11:41:13 AM]
Kaijin
Kaijin
quote:
Original post by Polymorphic OOP
No, actually you are wrong. It would be a member function pointer if he did

&a::count

His expression, a.count without parenthesis is completely illegal C++ code and should generate a compiler error. It''s the fault of his compiler.


Ah yes, of course. You are right.

nonpop, I tried compiling your code using gcc and it wouldn''t compile. I guess it must be a bug with the compiler.

- Kaijin
Greatwolf
Greatwolf
quote:
Original post by Kaijin

Ah yes, of course. You are right.

nonpop, I tried compiling your code using gcc and it wouldn''t compile. I guess it must be a bug with the compiler.

- Kaijin


Of course it''s the fault of the compiler, he''s using VC++ 6!
I did a quick test with the sample code w/ borland''s compiler and it complains about the syntax as well.





--{You fight like a dairy farmer!}
--{You fight like a dairy farmer!}
OleKaiwalker
OleKaiwalker
Nice, quite the thread here...

Thx for the answers, even though some of them were a bit off topic
Drevay
Drevay
int count () const <-- uhhmm...


_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.