Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

OleKaiwalker

Const

This topic is 5316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yeah, f.e if you try to access private data of your class/structure in const method you will get compiler error.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by 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 &lt;iostream&gt;
using namespace std;

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

int main ()
{
a ca;
cout &lt;&lt; "ca.count() = " &lt;&lt; ca.count &lt;&lt; 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)

<SPAN CLASS=editedby>[edited by - nonpop on February 28, 2004 9:50:10 AM]</SPAN>


<SPAN CLASS=editedby>[edited by - nonpop on February 28, 2004 9:53:59 AM]</SPAN>



That''s valid C++. Nowhere in your code your altering member variables[/I] in const method.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!