D
David K in San Jose
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that
form I'm trying to invoke some static functions by using an array of function pointers (delegates). I assume I need to
use the array< T > keyword to allocate an array of delegates, and then initialize the array by setting each array
element to the pointers (handles) of the functions I'll be invoking.
I've been trying to figure this out for several hours, and can't find any examples of how to create and initialize an
array of function delegates in managed C++. I'm hoping someone can post a tiny example that actually works.
Below is an example of what I'm trying to do. I want to be able to invoke the functions TestFunc1(), TestFunc2() and
TestFunc3() through an array of delegates to those functions. In the example, I show that I'm able to create a single
delegate TestFunc that points at TestFunc1(), but when I try to create an array of delegates, I get compiler errors
shown in the comments below:
- - Start of code excerpt - - - - -
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
namespace MyApp {
public ref class MyForm : public System::Windows::Forms::Form
{
// ...snip... Designer-created initialization code for the Windows Form
private:
// Declare the delegate to a test function that takes two integers and returns nothing:
delegate void TestFuncDelegate (int x, int y);
// As a demonstration, instantiate one delegate to TestFunc1():
// (This compiles correctly.)
static TestFuncDelegate ^TestFunc = gcnew TestFuncDelegate( TestFunc1 );
// Now try to instantiate an array of three delegates to TestFunc1(), TestFunc2()
// and TestFunc3():
//
// At compile time, this gives error C3374 and three error C2440s:
// error C3374: can't take address of 'MyApp::MyForm::TestFunc1' unless creating delegate instance
// error C2440: 'initializing' : cannot convert from 'void (__clrcall*)(int,int)' to
MyApp::MyForm::TestFuncDelegate ^'
//
static array<TestFuncDelegate ^> ^TestFuncArray = gcnew array<TestFuncDelegate ^> {
TestFunc1,
TestFunc2,
TestFunc3
};
// Define the three functions we'll call using the delegate:
static void TestFunc1( int x, int y) { return; }
static void TestFunc2( int x, int y) { return; }
static void TestFunc3( int x, int y) { return; }
// ...snip...
} // class Charting
} // namespace MyApp
- - End of code excerpt - - - - -
So I get the error "can't take address of 'TestFunc1' unless creating delegate instance". Maybe I'm wrong in trying to
use the array<> keyword? Or do I have some dereferencing error?
As a 2nd attempt, I'm able to successfully allocate an array of delegates this way:
static array<TestFuncDelegate ^> ^TestFuncArray = gcnew array<TestFuncDelegate ^>(3);
....but I can't figure out how to initialize the TestFuncArray[] at run-time. When I try something like this in the
initialization code for my form:
TestFuncArray[0] = &TestFunc1;
....I get these two compiler errors:
error C3867: 'MyApp::MyChart::TestFunc1': function call missing argument list; use '&MyApp::MyForm::TestFunc1' to
create a pointer to member
error C2440: '=' : cannot convert from 'void (__clrcall MyApp::MyForm::* )(int,int)' to
'MyApp::MyForm::TestFuncDelegate ^'
Any help is appreciated,
David K in San Jose
form I'm trying to invoke some static functions by using an array of function pointers (delegates). I assume I need to
use the array< T > keyword to allocate an array of delegates, and then initialize the array by setting each array
element to the pointers (handles) of the functions I'll be invoking.
I've been trying to figure this out for several hours, and can't find any examples of how to create and initialize an
array of function delegates in managed C++. I'm hoping someone can post a tiny example that actually works.
Below is an example of what I'm trying to do. I want to be able to invoke the functions TestFunc1(), TestFunc2() and
TestFunc3() through an array of delegates to those functions. In the example, I show that I'm able to create a single
delegate TestFunc that points at TestFunc1(), but when I try to create an array of delegates, I get compiler errors
shown in the comments below:
- - Start of code excerpt - - - - -
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
namespace MyApp {
public ref class MyForm : public System::Windows::Forms::Form
{
// ...snip... Designer-created initialization code for the Windows Form
private:
// Declare the delegate to a test function that takes two integers and returns nothing:
delegate void TestFuncDelegate (int x, int y);
// As a demonstration, instantiate one delegate to TestFunc1():
// (This compiles correctly.)
static TestFuncDelegate ^TestFunc = gcnew TestFuncDelegate( TestFunc1 );
// Now try to instantiate an array of three delegates to TestFunc1(), TestFunc2()
// and TestFunc3():
//
// At compile time, this gives error C3374 and three error C2440s:
// error C3374: can't take address of 'MyApp::MyForm::TestFunc1' unless creating delegate instance
// error C2440: 'initializing' : cannot convert from 'void (__clrcall*)(int,int)' to
MyApp::MyForm::TestFuncDelegate ^'
//
static array<TestFuncDelegate ^> ^TestFuncArray = gcnew array<TestFuncDelegate ^> {
TestFunc1,
TestFunc2,
TestFunc3
};
// Define the three functions we'll call using the delegate:
static void TestFunc1( int x, int y) { return; }
static void TestFunc2( int x, int y) { return; }
static void TestFunc3( int x, int y) { return; }
// ...snip...
} // class Charting
} // namespace MyApp
- - End of code excerpt - - - - -
So I get the error "can't take address of 'TestFunc1' unless creating delegate instance". Maybe I'm wrong in trying to
use the array<> keyword? Or do I have some dereferencing error?
As a 2nd attempt, I'm able to successfully allocate an array of delegates this way:
static array<TestFuncDelegate ^> ^TestFuncArray = gcnew array<TestFuncDelegate ^>(3);
....but I can't figure out how to initialize the TestFuncArray[] at run-time. When I try something like this in the
initialization code for my form:
TestFuncArray[0] = &TestFunc1;
....I get these two compiler errors:
error C3867: 'MyApp::MyChart::TestFunc1': function call missing argument list; use '&MyApp::MyForm::TestFunc1' to
create a pointer to member
error C2440: '=' : cannot convert from 'void (__clrcall MyApp::MyForm::* )(int,int)' to
'MyApp::MyForm::TestFuncDelegate ^'
Any help is appreciated,
David K in San Jose