Consider this
class Foo
{
public:
Foo(){}
~Foo(){}
void NonConstBar() {}
void ConstBar() const {}
};
int main()
{
const Foo* pFoo = new Foo();
pFoo->ConstBar(); //No error
pFoo->NonConstBar(); //Compile error about non const function being invoked
delete pFoo; //No error
return 0;
}
In the main function I am calling both const and non const functions of Foo
Trying to call any non const function yields an error in Visual Studio like so
error C2662: 'Foo::NonConstBar' : cannot convert 'this' pointer from 'const Foo' to 'Foo &'
But delete pFoo
doesn't issue any such error. The delete statement is bound to call the destructor of Foo class which doesn't have a const modifier. The destructor is also allowed to call other non const member functions. So is it a const function or not ? Or is delete on a const pointer a special exception?