🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Ronald van Loon Wed, 27 Jan 1993 14:31
Pointer to functions and const-ness
#1384
Author: Ronald van Loon
Date: Wed, 27 Jan 1993 14:31
51 lines
2235 bytes
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"
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads