Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Pointer to memberfunction for managed code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank Vanris - 03 Mar 2005 21:00 GMT
Hi,

I have the following code:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace std;

__gc class A {
public:
   A() : _value(0) {}
   ~A() {}

   void setValue(int value) {
       _value = value;
   }
   int getValue() {
       return _value;
   }

private:
   int _value;
};

int _tmain()
{
   A* a = new A();
   void (A::*func)(int);
   func = &A::setValue;

   (a->*func)(42);
   cout << a->getValue() << endl;
   delete a;
   return 0;
}

This compiles and runs fine when I declare A to be unmanaged (i.e. without
the __gc), but when I make it managed it gives me the following compile
error:

c:\Frank\Develop\BoostTest\BoostTest.cpp(26): error C2843: 'A' : cannot take
the address of a non-static data member or method of a managed type

which is at the location where I declare the func (so not in the assignment
one line lower).

Does this mean I cannot have pointer to memberfunction for managed code? Or
is there another way I can have a pointer to memberfunction?

Frank.
Tomas Restrepo \(MVP\) - 03 Mar 2005 23:46 GMT
Frank,

> I have the following code:
>
[quoted text clipped - 43 lines]
> Does this mean I cannot have pointer to memberfunction for managed code? Or
> is there another way I can have a pointer to memberfunction?

There are delegates for managed code, which are a more clean and the
preferable alternative.

Why do you need a pointer to the member function?

Signature

Tomas Restrepo
tomasr@mvps.org

Frank Vanris - 04 Mar 2005 15:53 GMT
Delegates! I didn't know that. Thanks.

The reason I wanted to use pointer to memberfunction was that I wanted to
use the boost library, especially the bind functions which accept a pointer
to memberfunction.

I will try to use the delegates instead.

Frank.

> Frank,
>
[quoted text clipped - 54 lines]
>
> Why do you need a pointer to the member function?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.