const correctness

Posted by ra170 on Stack Overflow See other posts from Stack Overflow or by ra170
Published on 2010-06-03T16:11:20Z Indexed on 2010/06/03 16:14 UTC
Read the original article Hit count: 323

Filed under:
|

I was going through: C++ FAQs about inheritance and decided to implement it (just to learn it)

#include "Shape.h"

 void Shape::print() const
 {
     float a = this->area();  // area() is pure virtual
     ...
 } 

now, everything (well, almost) works as described in item: faq:23.1 except that print() is const and so it can't access the "this" pointer, as soon as you take out const, it works. Now, C++ FAQs have been around for a while and are usually pretty good. Is this a mistake? Do they have typo or am I wrong? If I'm wrong, I would like to know how is it possible to access the "this" pointer in a const function.

© Stack Overflow or respective owner

Related posts about c++

Related posts about const