mutable keyword
mutable keyword :
-----------------------
Class member functions may be declared as const, which causes this to be treated as a
const pointer. Thus, that function cannot modify the object that invokes it. Also, a const
object may not invoke a non-const member function. However, a const member
function can be called by either const or non-const objects.
within const member fns we can' change the value. In C++ we can change the value using mutable keyword.
class X
{
int i;
public:
int f1() const; // const member function
};
int X::f1() const
{
i = 10; // This will gives error because we are trying to modify the value in a const member function
return i;
}
we can modify the values if we use the mutable keyword. Following code will works...
This is the concept of mutable in C++.
class X
{
mutable int i;
public:
int f1() const; // const member function
};
int X::f1() const
{
i = 10; // This will gives error because we are trying to modify the value in a const member function
return i;
}
-----------------------
Class member functions may be declared as const, which causes this to be treated as a
const pointer. Thus, that function cannot modify the object that invokes it. Also, a const
object may not invoke a non-const member function. However, a const member
function can be called by either const or non-const objects.
within const member fns we can' change the value. In C++ we can change the value using mutable keyword.
class X
{
int i;
public:
int f1() const; // const member function
};
int X::f1() const
{
i = 10; // This will gives error because we are trying to modify the value in a const member function
return i;
}
we can modify the values if we use the mutable keyword. Following code will works...
This is the concept of mutable in C++.
class X
{
mutable int i;
public:
int f1() const; // const member function
};
int X::f1() const
{
i = 10; // This will gives error because we are trying to modify the value in a const member function
return i;
}
Labels: Cpp
0 Comments:
Post a Comment
<< Home