linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* function pointers in C++
@ 2004-03-15  9:28 elathan
  2004-03-15 10:27 ` Jan-Benedict Glaw
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: elathan @ 2004-03-15  9:28 UTC (permalink / raw)
  To: linux-c-programming

Hello! 
 
Suppose I have: 
 
class Foo 
{ 
public: 
	Foo(); 
	~Foo(); 
	int bar(int); 
}; 
 
Now, why I can't do this: 
 
Foo *f = new Foo(); 
taz(f->bar); 
 
Where taz()'s protype is: 
 
void taz(int (*f)(int)); 
 
In plain C, I can do: 
 
int f(int) { ... } 
taz(f); 
 
without a problem. Is there a way to accomplish such a thing (i.e. 
to pass the pointer of a C++ class's member function to another 
function that expects a pointer to function as an argument)? 
 
Regards, 
--  


^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: function pointers in C++
@ 2004-03-15 14:21 Sandro Dangui
  2004-03-15 11:28 ` Elias Athanasopoulos
  0 siblings, 1 reply; 11+ messages in thread
From: Sandro Dangui @ 2004-03-15 14:21 UTC (permalink / raw)
  To: elathan; +Cc: linux-c-programming


I think this sample code will help you... Verify if it looks brilliant
enough. :-))

----------------------------------------------------------------------------
--------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

// definitions for portability
#ifdef _WIN32
#define STDCALL __cdecl
#else
#define STDCALL
#endif

class Foo
{
public:
	Foo(int id)
	{
		Id = id;
	}

	int STDCALL bar(int value)
	{
		printf("Method Foo::bar with param value=%i, Foo Id=%i\n",
value, Id);

		return (value * 2 + Id);
	}

protected:
	int Id;
};

class CallFooMethod
{
public:
	CallFooMethod(void* pInstance, ...)
	{
		va_list argList;

		va_start(argList, pInstance);

		pObject = pInstance;

		pMethod = va_arg(argList, void *);

		va_end(argList);
	}

	int CallIt(int value)
	{
		typedef int (*F)(void *, int);
		F f = (F) pMethod;

		/* The trick here is that the compiler pass the "this" value
as a hidden parameter... Then,
		   let's do the same thing explicitly */
		return f(pObject, value);
	}

protected:
	void* pObject;
	void* pMethod;
};

int main(int /*argc*/, char** /*argv*/)
{
	Foo foo1(1);
	Foo foo2(2);

	CallFooMethod callFooMethod1(&foo1, &Foo::bar);
	CallFooMethod callFooMethod2(&foo2, &Foo::bar);

	int retValue1 = callFooMethod1.CallIt(50);
	int retValue2 = callFooMethod2.CallIt(100);

	printf("retValue1=%i, retValue2=%i\n", retValue1, retValue2);

	return 0;
}
----------------------------------------------------------------------------
--------------------

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of
elathan@phys.uoa.gr
Sent: Monday, March 15, 2004 7:40 AM
To: Jan-Benedict Glaw
Cc: linux-c-programming@vger.kernel.org
Subject: Re: function pointers in C++


 
Jan-Benedict Glaw <jbglaw@lug-owl.de>: 
> On Mon, 2004-03-15 11:28:19 +0200, elathan@phys.uoa.gr 
> <elathan@phys.uoa.gr>
> > void taz(int (*f)(int));
>  
> You're looking for "static"ally declared member functions.
 
And in that case I can't use 'this' inside the function. Brilliant... :-( 
 
Thanx for the reply. 
 
Regards, 
--  

-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: function pointers in C++
@ 2004-03-15 21:59 Sandro Dangui
  0 siblings, 0 replies; 11+ messages in thread
From: Sandro Dangui @ 2004-03-15 21:59 UTC (permalink / raw)
  To: Elias Athanasopoulos; +Cc: linux-c-programming


You got this error: converting from `double (Foo::*)(double*, double*)' to
`double (*)(double*, double*)' because the compiler doesn't know which
instance of class Foo it should use in the call... As I said before the
compiler pass the "this" value (that represents an instance of a class) as a
hidden parameter.

So, the only possible solution that I can see is the use of static method.
Do you need multiple instances of the class that implements the desired
method? Or can you use a singleton? Using a singleton your static method
will have access to other methods and attributes through the static method
getInstance().

Sandro.



-----Original Message-----
From: Elias Athanasopoulos [mailto:elathan@phys.uoa.gr] 
Sent: Monday, March 15, 2004 8:29 AM
To: Dangui, Sandro [CMPS:RY11:EXCH]
Cc: linux-c-programming@vger.kernel.org; Dar?o Mariani
Subject: Re: function pointers in C++


On Mon, Mar 15, 2004 at 11:21:18AM -0300, Sandro Dangui wrote:
> 
> I think this sample code will help you... Verify if it looks brilliant 
> enough. :-))

Thanks for the detailed reply. 

Actually, what I want to do is to pass a pointer to a member function in a
function that expects a pointer to function; I don't want to call the member
function, but just pass its address.

Following Stroustrup (pages 156..158), I did something like this:

    Foo *f = new Foo(id);
    double (Foo::*pfunc)(double *, double *) = &Foo::func;
    Bar *res = new Bar((double (*)(double *, double *))(func->*pfunc));

And:

class Foo {
public:
    Foo(int id);
    ~Foo();
    double func(double *, double *);
};

Also Bar's constructor (I can't change it --third party implementation) is:

    Bar(double (*)(double *, double *));

Amazingly I get the error:

test.cxx:107: error: converting from `double (Foo::*)(double*, double*)' to 
   `double (*)(double*, double*)'
   
which is *at least* annoying. It is another BIG suprise from C++. I mean,
there is, most probably, a good reason why this is not supported, but it is
a *broken design*.

Foo::func()'s prototype is (double) (double *, double *), which in my human
brain is compiled as: 'give me two pointers to double and I will give you
one double'. Why I can't pass it to a function which *expects* what my poor
human brain understands?

I didn't ask to convert for me a string to float...

Regards,
-- 
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 

	

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-03-18 11:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-15  9:28 function pointers in C++ elathan
2004-03-15 10:27 ` Jan-Benedict Glaw
2004-03-15 10:39   ` elathan
2004-03-15 14:38 ` Darío Mariani
2004-03-15 14:56   ` Darío Mariani
2004-03-15 19:10 ` Ashutosh Ranjan
  -- strict thread matches above, loose matches on Subject: below --
2004-03-15 14:21 Sandro Dangui
2004-03-15 11:28 ` Elias Athanasopoulos
2004-03-16  7:50   ` Glynn Clements
2004-03-18 11:17     ` Elias Athanasopoulos
2004-03-15 21:59 Sandro Dangui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).