Must the predicate be a static member function?

B

BG

Here's my program.

{
array<Byte> ^buffer = // something
int length = Array::FindIndex(buffer, gcnew
Predicate<Byte>(&MyClass::isChar13) );
...
}

bool MyClass::isChar13(Byte b)
{
return b == 13;
}

This gives the error
error C3352: 'bool MyClass::isChar13(unsigned char)' : the specified
function does not match the delegate type 'bool (unsigned char)'

Now if I rewrite the predicate function like so

static bool MyClass::isChar13(Byte b) // note: made it static
{
return b == 13;
}

without changing the call to it, it compiles fine.

Can anyone explain what's happening? And must I make the predicate static?
Is there another way?

Thanks in advance.
 
C

Carl Daniel [VC++ MVP]

BG said:
Here's my program.

{
array<Byte> ^buffer = // something
int length = Array::FindIndex(buffer, gcnew
Predicate<Byte>(&MyClass::isChar13) );

try Array.FindIndex(buffer, gcnew(Predicate<Byte>(this,&MyClass::isChar13);

(assuming this code appears inside another member function MyClass, that
is).

-cd
 
B

BG

Carl Daniel said:
try Array.FindIndex(buffer,
gcnew(Predicate<Byte>(this,&MyClass::isChar13);

(assuming this code appears inside another member function MyClass, that
is).

-cd
Hi Carl,
Thanks for your reply.
I tried what you suggested but it didn't compile. Gave the error:
warning C4832: token '.' is illegal after UDT 'System::Array'
g:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see
declaration of 'System::Array'
error C2275: 'System::Array' : illegal use of this type as an expression
g:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see
declaration of 'System::Array'
error C3352: 'bool MyClass::isChar13(unsigned char)' : the specified
function does not match the delegate type 'bool (unsigned char)'

The last error suggested that the compiler does not expect the function to
be a member of a class. I made appropriate changes and it compiled fine.
But I didn't like this solution. What if I wanted to make a class member
function my predicate? So again, taking a cue from the last error, I read
the documentation on delegates. (Being new to C++/CLI. I still don't many of
these basic things that you probably take for granted).

I then made the function a member of the class again, but changed the call
to it like so
int length = Array::FindIndex(buffer, gcnew Predicate<Byte>(this,
&MyClass::isChar13) );
That is, introduced 'this' as the first arg to Predicate ctor.
That worked!
I am still looking at the documentation to fully understand the reasons. But
if you do, please do post.
 
C

Carl Daniel [VC++ MVP]

BG said:
"Carl Daniel [VC++ MVP]"
try Array.FindIndex(buffer,
gcnew(Predicate<Byte>(this,&MyClass::isChar13);

I then made the function a member of the class again, but changed the
call to it like so
int length = Array::FindIndex(buffer, gcnew Predicate<Byte>(this,
&MyClass::isChar13) );
That is, introduced 'this' as the first arg to Predicate ctor.
That worked!
I am still looking at the documentation to fully understand the
reasons. But if you do, please do post.

Yeah, that's what I inteded to write, but I left off a right-parenthesis.

A delegate combines two things:
- a method selector, in C++ this is expressed as a pointer-to-member
- an object reference.

Every delegate always has both parts. Now, if now object reference was
supplied, then it is assumed that the method must be static since that's the
only kind of method that can be called without an object reference. Since
you wanted a non-static method, you need to supply the object reference.

In standard C++ terms, you can think of a delegate as looking something like
this:

template <class T, class R, typename R (T::*PMF)(object, EventArgs)>
class Delegate
{
private:
T* m_t;
PMF m_pmf;

public:
R Invoke(object sender, EventArgs e)
{
return (m_t->*pmf)(sender,e);
}
};

.... with the caveat that, unlike standard C++, m_pmf can refer to a static
function, and it if is a static function, then m_t can be null.

In practice, a CLR delegate type is actually quite a bit more complex than
this, but this should give you an idea of what's going on under the covers.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top