answersLogoWhite

0


Best Answer

A constant member function is an instance method that can only modify the mutable members of the class instance. This is achieved by implicitly declaring the this pointer constant. The this pointer is a hidden parameter that is implicitly passed to every instance function, and that points to the current instance of the class. Static member functions do not have an implicit this pointer and therefore cannot be declared const.

Consider the following simple class that has both mutable and non-mutable member variables, and both constant and nonconstant member functions:

struct foo

{

void constant()const;

void nonconstant();

private:

int data1;

mutable int data2;

};

void foo::constant()const

{

data1++; // not permitted: the implicit this pointer is declared const

data2++; // ok: data2 is mutable

}

void foo::nonconstant()

{

data1++; // ok: the implicit this pointer is declared non-const

data2++; // ok: data2 is mutable

}

Note that data2 can always be modified since it is declared mutable, but data1 can only be modified by nonconstant member functions. Members should only be declared mutable when they are only used internally by a class (such as when locking a mutex for thread safety), or where a value needs to be calculated and cached the first time it is accessed.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a constant member function c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


What is message passing in c plus plus?

-define class with necessary data member & member function. -create object of that class. -communication.


Can you use the same function name for member function in c plus plus?

Yes. (And of course you could simply try it, instead of asking.)


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


What is self referential function in c plus plus?

A self-referential function in C++, or in any other supporting language, is a recursive function.


What are the two types of constant in c plus plus?

Constant data and constant functions.


Why can't the accessor member function change any of the values in a class in C plus plus?

Nothing stops a member function from changing any of the values in a class. By convention, an accessor function is used to give read only access to class data, but that does not mean that it is prohibited from doing so. It is a member function, after all, and it has all the rights of any member function of the class.


What does a constant do in c plus plus?

A constant is a variable that does not change. The correct term is constant variable.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


What is the meaning of x.getdata in c plus?

it mens u r calling de member function of de program


How constant are used in c?

it is predefined function


Can a Horizontal line represent a function?

Yes. It is the function f(x) = c where c is a constant.