473,779 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Must the predicate be a static member function?

BG
Here's my program.

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

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

This gives the error
error C3352: 'bool MyClass::isChar 13(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::isChar 13(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 5580
BG wrote:
Here's my program.

{
array<Byte> ^buffer = // something
int length = Array::FindInde x(buffer, gcnew
Predicate<Byte> (&MyClass::isCh ar13) );


try Array.FindIndex (buffer, gcnew(Predicate <Byte>(this,&My Class::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.nos pam>
wrote in message news:eW******** ******@TK2MSFTN GP10.phx.gbl...
BG wrote:
Here's my program.

{
array<Byte> ^buffer = // something
int length = Array::FindInde x(buffer, gcnew
Predicate<Byte> (&MyClass::isCh ar13) );


try Array.FindIndex (buffer,
gcnew(Predicate <Byte>(this,&My Class::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\micr osoft.net\frame work\v2.0.50727 \mscorlib.dll : see
declaration of 'System::Array'
error C2275: 'System::Array' : illegal use of this type as an expression
g:\windows\micr osoft.net\frame work\v2.0.50727 \mscorlib.dll : see
declaration of 'System::Array'
error C3352: 'bool MyClass::isChar 13(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::FindInde x(buffer, gcnew Predicate<Byte> (this,
&MyClass::isCha r13) );
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,&My Class::isChar13 );


I then made the function a member of the class again, but changed the
call to it like so
int length = Array::FindInde x(buffer, gcnew Predicate<Byte> (this,
&MyClass::isCha r13) );
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)(objec t, 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
4618
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
4
3350
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 if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added and gives the error message (in Borland C++Builder 6) Unit1.cpp E2285 Could not find a match for
2
1629
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); } bool MyClass::UniquePredicate(const CFPrint& x, const CFPrint& y) {
5
7359
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 std::list::sort with such a predicate but that doesn't work:
15
6594
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
651
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
2
1916
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
78097
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? Thanks.
3
6130
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. Copy and paste it into VS2008 for a Windows Forms application. With some effort I can do the job with a user defined function, but my question is whether I can do it using the built in library function "Array.Find" and a predicate that takes...
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.