473,915 Members | 3,885 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 5587
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
4640
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
3361
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
1637
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
7366
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
6611
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
1923
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
78107
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
6136
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
9883
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
11359
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10928
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
11069
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
10543
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...
1
8102
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7259
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4346
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.