471,595 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Must the predicate be a static member function?

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.
Jan 22 '06 #1
3 5471
BG wrote:
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
Jan 22 '06 #2
BG

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:eW**************@TK2MSFTNGP10.phx.gbl...
BG wrote:
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

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\msco rlib.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\msco rlib.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.

Jan 22 '06 #3
BG wrote:
"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
Jan 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matthias_k | last post: by
15 posts views Thread by Samee Zahur | last post: by
5 posts views Thread by Tony Johansson | last post: by
2 posts views Thread by Marco Segurini | last post: by
8 posts views Thread by Jeff S. | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.