Topic: Pointer to functions and const-ness


Author: rvloon@cv.ruu.nl (Ronald van Loon)
Date: 27 Jan 93 14:31:02 GMT
Raw View
A question to all C++ gurus out there:

Consider the following class definition:

class test
{
  private:
    int iPrivateInteger;

  public:
    virtual void f(void (*)(int&));
    virtual void f(void (*)(const int&)) const;

    virtual void g();
    virtual void h() const;
};

The purpose of the two definitions of f (note the placing of const) is to pass
the integer 'iPrivateInteger' to the function that is passed as an argument.

Obviously, when a const object of this class is used, one does not want the
internal integer to change, hence the definition of the prototype is different
as well.

Say I have two functions:

void non_const_function(int&)
{
}

void const_function(const int&)
{
}

The first one matches the prototype of the first 'f', the second one of the
second 'f'. So, if I pass 'const_function' to an object of this class, the
second 'f' should be called, and conversely if I pass 'non_const_function' the
first 'f' should be called.

The question is: is that correct ? Intuitively I would say yes, but maybe
function-parameters do not take 'const' into account. (in effect :

void (*)(int&) is equivalent to void (*)(const int&)); another possibility is
that void(*)(const int&) can be implicitly converted to void (*)(int&).

Any opinions ?
--
Ronald van Loon     | Consider this: In the United States, an automobile is
(rvloon@cv.ruu.nl)  | stolen EVERY 14.7 SECONDS.
3DCV Group, Utrecht |   If that statistic scares you, think how we felt when we
The Netherlands     | made it up. - Dave Barry, "CHRISTMAS BUYERS GUIDE"