• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

C++

Hi,
In C++ , constatnt member function is not allowed to change the data in the data members. Is there any mechanism , to make constant member function , so that it will behave like an ordinary member function. I mean , like casting ..etc.,
0
Sekhar_ee
Asked:
Sekhar_ee
  • 2
1 Solution
 
SalteCommented:
Yes, you can both use casting but you probably shouldn't.

if you want to change data in a constant member function you really aren't honest, you are telling the user of your class that this function is constant so I won't change any data and then you go ahead and change them anyway. Nasty, nasty.

However, it is possible that the class has members that you want to change - even for constant member functions. This is typically members that doesn't really reflect the value or state of the object but rather are used for book keeping etc, reference counters are a typical example.

Declaring those members to be mutable says that "this member can be modified even by a constant member function" and I would assume that this is by far the best way to go.

class X {
private:
   mutable int a;
   int b;
   static int c;

public:
   static void foo()
   {
    ...can read and write c..
    ... cannot access a or b unless it has a pointer
    ... or reference or have otherwise access to an
    .. instance of X.
   }

   void bar() const
   {
     ..can read and write a and can read b..
     ... cannot modify b though.
     ... can read adn write c also
   }

   void baz()
   {
     ... can read and write a, b and c.
   }
};

My guess is that this is what you really want.

Alf
0
 
designedmindCommented:
Doing the nasty

template<v_t> class mrGrinch
{
  private:
  v_t * bar;
  public:
  void foo() const
  {
    v_t * nasty = const_cast(bar);
  //manipulate the pointer like the old dirty you are!
  }

}
0
 
SalteCommented:
doing const_cast on the this pointer is probably more what he was asking for.

However, as I stated in my previous point that is probably not what you want to do. Check out the 'mutable' keyword in C++ and what it does.

I think that is more what you REALLY want.

anyway, here is the const_cast of this pointer if you want it.

class foo {
private:
   ....members here...
   int m;
   mutable int m2;
public:
   ....
   void bar() const
   {
      foo * p = const_cast<foo *>(this);
      p -> m = 7; // modifying const object's member!
   }

   void baz() const
   {
      m2 = 5; // m2 can be modified - even by const functions!
   }
};

However, the same effect without the casting can be done by declaring the member as mutable as shown in function baz().

By far the mutable way is the cleanest way to achieve the effect.

Alf
0
 
Sekhar_eeAuthor Commented:
Hi,

Thanks for your solution.

I asked this question keeping const_cast in my mind.

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now