🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1384

Pointer to functions and const-ness

#1384
From: 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"
Technical Details
Message-ID: <1993Jan27.143102.20008@cv.ruu.nl>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!swrinde!cs.utexas.edu!sun-barr!news2me.EBay.Sun.COM!seven-up.East.Sun.COM!sungy!news.Sweden.Sun.COM!swippy!seunet!sunic!mcsun!sun4nl!ruuinf!prisma.cv.ruu.nl!rvloon

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!swrinde!cs.utexas.edu!sun-barr!news2me.EBay.Sun.COM!seven-up.East.Sun.COM!sungy!news.Sweden.Sun.COM!swippy!seunet!sunic!mcsun!sun4nl!ruuinf!prisma.cv.ruu.nl!rvloon
From: rvloon@cv.ruu.nl (Ronald van Loon)
Newsgroups: comp.std.c++,comp.lang.c++
Subject: Pointer to functions and const-ness
Message-ID: <1993Jan27.143102.20008@cv.ruu.nl>
Date: 27 Jan 93 14:31:02 GMT
Sender: usenet@cv.ruu.nl (Usenet Network)
Organization: University of Utrecht, 3D Computer Vision Research Group
Lines: 51
Originator: rvloon@triton.cv.ruu.nl
Nntp-Posting-Host: triton.cv.ruu.nl