473,398 Members | 2,343 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,398 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 5551
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
2
by: BCC | last post by:
What the heck am I doing wrong? void MyClass::JoinIdenticalFingerprints(CFPrintList& list) { CFPrintList unique_list = list; unique(unique_list.begin(), unique_list.end(), UniquePredicate); }...
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
2
by: Marco Segurini | last post by:
Hi, I have written the following code: // start code using System; using System.Collections.Generic; using System.Text; namespace MyPredicates
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
3
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.