473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problem with construction via a function pointer to a local method

Here is a simplification of my code. Basically, I have a class (A) that can
be constructed using a function pointer to a function that returns a bool
with no parameters. I then want to create an instance of this class (A) in
another class (B) which uses one of its own methods of the 'proper form' to
initialize the instance. But I get an error:

__gc class ClassA
{
public:
ClassA( bool (*func)(void) ) {//---some code---//} // constructor
} ;
__gc class ClassB
{
public:
void Method_B1( )
{
class_a = new ClassA( Method_B2 ) ; // error C2664
}

private:
bool Method_B2( ) {{//---some code---//}

ClassA* class_a ;
} ;

error C2664: 'ClassA::ClassA(bool (__cdecl *)(void))' : cannot convert
parameter 1 from 'bool (void)' to 'bool (__cdecl *)(void)

What is going on here, and what do I need to fix this (i.e., allow me to
pass such a method)?
Nov 17 '05 #1
6 1214
Hi Peter!
Here is a simplification of my code. Basically, I have a class (A) that can
be constructed using a function pointer to a function that returns a bool
with no parameters. I then want to create an instance of this class (A) in
another class (B) which uses one of its own methods of the 'proper form' to
initialize the instance. But I get an error:

For managed code you should use a delegate!!!
And no function pointers...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
Ok, cool, and thanks! I should use 'delegates'. However, since I've never
even heard of a 'delegate' before, could you give me an idea HOW to use
them? : )

And what's wrong with function pointers and managed code? So far it seems
managed stuff doesn't support many things, like 'const', 'static', etc, and
now function pointers? It seems like a whole new language should create
instead of trying to keep managed and unmanaged together if there are so
many incompatibilities between the two. I like managed stuff, but the more I
use it the more I keep falling into 'traps' (stuff I think should work
(since it does in unmanaged code) that generates errors)...

PS - I looked 'delegates' up in the on-line MSDN, but it doesn't seem to say
anything remotely like about how to pass a function of a given form to a
method (and I NEED to PASS different functions, I can't just hard code each
case, there are an inifinte number of such cases!).

"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:uA****************@TK2MSFTNGP14.phx.gbl...
Hi Peter!
Here is a simplification of my code. Basically, I have a class (A) that
can be constructed using a function pointer to a function that returns a
bool with no parameters. I then want to create an instance of this class
(A) in another class (B) which uses one of its own methods of the 'proper
form' to initialize the instance. But I get an error:

For managed code you should use a delegate!!!
And no function pointers...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #3
Actually, I re-read the MSDN stuff on delegates, and I think I'm getting the
hang of them. A delegate IS a function form, and you can create instances of
a delegate that are associated with a function of that form that executes
that function. Now one can pass a pointer to the delegate, and this is
equivalent to passing a 'function pointer' of the original function.

Did that make sense?

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Ok, cool, and thanks! I should use 'delegates'. However, since I've never
even heard of a 'delegate' before, could you give me an idea HOW to use
them? : )

And what's wrong with function pointers and managed code? So far it seems
managed stuff doesn't support many things, like 'const', 'static', etc,
and
now function pointers? It seems like a whole new language should create
instead of trying to keep managed and unmanaged together if there are so
many incompatibilities between the two. I like managed stuff, but the more
I
use it the more I keep falling into 'traps' (stuff I think should work
(since it does in unmanaged code) that generates errors)...

PS - I looked 'delegates' up in the on-line MSDN, but it doesn't seem to
say
anything remotely like about how to pass a function of a given form to a
method (and I NEED to PASS different functions, I can't just hard code
each
case, there are an inifinte number of such cases!).

"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in
message news:uA****************@TK2MSFTNGP14.phx.gbl...
Hi Peter!
Here is a simplification of my code. Basically, I have a class (A) that
can be constructed using a function pointer to a function that returns a
bool with no parameters. I then want to create an instance of this class
(A) in another class (B) which uses one of its own methods of the
'proper form' to initialize the instance. But I get an error:

For managed code you should use a delegate!!!
And no function pointers...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/


Nov 17 '05 #4

Peter Oliphant wrote:
Actually, I re-read the MSDN stuff on delegates, and I think I'm getting the
hang of them. A delegate IS a function form, and you can create instances of
a delegate that are associated with a function of that form that executes
that function. Now one can pass a pointer to the delegate, and this is
equivalent to passing a 'function pointer' of the original function.

Did that make sense?


That's basically the idea... A delegate is an object that is callable
using a given signature, and the delegate can really represent a "call"
to a member function (static or not) or another "callable" entity (such
as another delegate).

Also, you code was wrong because it is buggy in *unmanaged* C++, this
has nothingto do with manged stuff : you can't use a non-static member
fuction pointer instead of a free function pointer (they do not have
the same representation : a non-static member function pointer is most
of the time bigger).
You can use a pointer to a static member function instead of a free
function pointer (although it is not guaranteed to work by the
standard, it is ok on all architectures I am aware of).

Concerning managed code, you *can* use function pointers in managed
code (well, pointers to unmanged functions that is), but you must obey
the rules of unmanaged C++.

Arnaud
MVP - VC

Nov 17 '05 #5
Actually, now that I've used them, I really like delegates. Makes the code
look a lot better than the format of function pointers... : )

<ad******@club-internet.fr> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...

Peter Oliphant wrote:
Actually, I re-read the MSDN stuff on delegates, and I think I'm getting
the
hang of them. A delegate IS a function form, and you can create instances
of
a delegate that are associated with a function of that form that executes
that function. Now one can pass a pointer to the delegate, and this is
equivalent to passing a 'function pointer' of the original function.

Did that make sense?


That's basically the idea... A delegate is an object that is callable
using a given signature, and the delegate can really represent a "call"
to a member function (static or not) or another "callable" entity (such
as another delegate).

Also, you code was wrong because it is buggy in *unmanaged* C++, this
has nothingto do with manged stuff : you can't use a non-static member
fuction pointer instead of a free function pointer (they do not have
the same representation : a non-static member function pointer is most
of the time bigger).
You can use a pointer to a static member function instead of a free
function pointer (although it is not guaranteed to work by the
standard, it is ok on all architectures I am aware of).

Concerning managed code, you *can* use function pointers in managed
code (well, pointers to unmanged functions that is), but you must obey
the rules of unmanaged C++.

Arnaud
MVP - VC

Nov 17 '05 #6
Peter Oliphant wrote:
Actually, now that I've used them, I really like delegates. Makes the
code look a lot better than the format of function pointers... : )


I agree that delegates (or "closures" or whatever you like to call them) are
a great thing that should be part of C++ IMHO. In unmanaged C++, there are
alternatives using libraries such as boost::signal or LibSigC++ that provide
the same facilities.

Arnaud
MVP - VC
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
4
by: Timothy Madden | last post by:
Hello everybody ! I have a function in a dll that will write bytes of data in a log file. The function has a local static FILE pointer like this: void VMLogPacket(BYTE *pData, size_t nSize) {...
5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
14
by: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ....then a bitwise object is created w/o calling...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
19
by: Chocawok | last post by:
Some of the classes in my app are graphical. To encapsulate the graphical side of things I had created a class called "sprite" which holds a bit map and knows how to draw itself etc. The...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.