About const member functions?

Book says that if we add a const following a function declaration, it will make it a const member function

bool same_isbn(const Sales_item &rhs) const;

will be compiler treated as

bool same_isbn(const Sales_item *const this, const Sales_item & rhs) const;

so that will make "this" pointer a const pointer which point to const Sales_item?
That is to say, compiler will add two "const" upthere?

What special usage of this kind of member function? Thanks.
It is a way to let the compiler know that you do not change the object's data. This lets the compiler do fancy optimizations, and lets you use the object in places that it otherwise could not be or wouldn't be allowed to be.

In other words, it is your way of saying "I guarantee I will not change the object's state with this function."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct point
  {
  double x, y;

  // Getting the angle from the +X axis does not need to 
  // change the object -- this is a constant function.
  double angle() const
    {
    return atan2( y, x );
    }

  // Setting the angle -- that requires a change
  // to the data, so this function is not const.
  void angle( double theta )
    {
    double s = sin( theta );
    double c = cos( theta );
    double xx = x*c - y*s;
    double yy = x*s + y*c;
    x = xx;
    y = yy;
    }
  };

Hope this helps.
Thanks, However,

If I only want to not change the object's value, then

bool same_isbn(const Sales_item * this, const Sales_item & rhs) const;

will be enough. no need to be:

bool same_isbn(const Sales_item *const this, const Sales_item & rhs) const;

I mean how could people change the "this" pointer's value when they simply call a member function in any way, right?
1
2
Sales_item a, b ;
s.same_isbn(b);
Last edited on
The this pointer is always assumed to be const, whether the function is or not. There isn't ever a valid reason to change it to point to something else.

bool same_isbn(const Sales_item &rhs) const;
without the const:
bool same_isbn(const Sales_item &rhs);
would be equivalent to:
bool same_isbn(Sales_item*const this, const Sales_item& rhs);

http://ideone.com/Ao1VbW

Hello,

Where you put the 'const' is important. Here are some possibilities (from Scott Meyers 1st book 'Effective C++'):

1
2
3
4
5
6
7
8
9
char greeting = "Hello"

char *p greeting; // non-const pointer, non const data

const char *p = greeting; // non-const pointer, const data

char* const p = greeting; // const pointer, non-const data

const char* const p = greeting; // const pointer, const data 


Largins
It doesn't make a constant member function. It creates a member function that treats the member variables of the class as constant.

Just being more specific.

1
2
3
void calc_somthing() const;  //can not change member variables

void calc_somthing(); //can change member variables 


When we talk about pointers, you need to remember that there is a difference between the pointer, and the data that they store. The pointer is constant, but the data it stores may not be.

for example:
1
2
3
char* const ch(new char('a'));

*ch = 'b'; //this is legal, because I'm not changing the pointer, just the data it stores. 


God luck.
Last edited on
Thanks!
and lets you use the object in places that it otherwise could not be or wouldn't be allowed to be.

To explain, consider this:
1
2
3
4
5
6
7
8
9
10
11
struct square
{
  int width;

  int area( void ){ return width * width; }

  friend ostream& operator<<( ostream& out, const square& sq )
  {
    return out << "Area: "  << sq.area(); // not legal, square is const
  }
};


Where as if the area was defined:
int area( void ) const;
It would be legal.
Last edited on
Topic archived. No new replies allowed.