BUG: Pointer to Member Allows Non-const Method Call on const Object (263589)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q263589

SYMPTOMS

No error or warning message is generated if you try to call a non-const member function on a const class object by using a member pointer.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

The following sample code demonstrates the bug:
//test.cpp
//compiler option needed: none
class A
{
  int  m_val;
 public:
   A(int i=0) : m_val(i) {}
   void Add(int val) { m_val+=val; }
};
 
int main()
{
	void(A::*MFptr)(int) = &A::Add;
	const A ca;      //const object
 	(ca.*MFptr)(5);  //no error here
	return 0;
}
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbBug kbCompiler kbCPPonly KB263589