🔒 Locked
Const
Advertisement
Original Post
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; }
It protects the invoking object that is calling that function.
Meaning, that the member data of the class can''t be altered?
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.
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.
You can also mark data members as
- Kaijin
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
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 
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]
#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]
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
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
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.
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:
So the value is used to determine which member function to call
- Kaijin
(object.*func_ptr)(10);
So the value is used to determine which member function to call
- Kaijin
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:
The output is:
// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
[edited by - neoztar on February 28, 2004 11:41:13 AM]
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]
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
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!}
Nice, quite the thread here...
Thx for the answers, even though some of them were a bit off topic
Thx for the answers, even though some of them were a bit off topic
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement