?
Solved

C++

Posted on 2003-03-05
4
Medium Priority
?
190 Views
Last Modified: 2010-05-18
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
Comment
Question by:Sekhar_ee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8071275
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
 

Expert Comment

by:designedmind
ID: 8085989
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
 
LVL 12

Accepted Solution

by:
Salte earned 150 total points
ID: 8086905
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
 

Author Comment

by:Sekhar_ee
ID: 8101583
Hi,

Thanks for your solution.

I asked this question keeping const_cast in my mind.

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question