Hi,
I'd like to use templates with event handlers. Appreciate any help. Here is the code. The problem is with the hook and unhook methods.
#include <stdafx.h>
template
<class T>class
PropertyChangeListener {public
: void propertyChange( std::string oldValue, std::string newValue ) {printf("Changed from " + oldValue + " to " + newValue );
}
void hookEvent(T* bean) { __hook(&T::MyEvent, bean, &PropertyChangeListener<T>::propertyChange);}
void unhookEvent(T* bean) { __unhook(&T::MyEvent, bean, &PropertyChangeListener<T>::propertyChange);}
};
class
Bean : public PropertyChangeListener<Bean> {std::string name;
typedef
PropertyChangeListener<Bean> listener;public
: __event void MyEvent(std::string value);
public
: void setName( std::string name ){listener::hookEvent(
this); __raise this->MyEvent( name );}
};
int
main() {Bean bean;
bean.setName( "Test" );
}
Thanks,
Mohan

templates with VC++ event handling
Kaspergyselinck
Thanks,
Ayman Shoukry
VC++ Team
shakinbacon
Hi Mohan,
The compiler errors in your code are giving pretty good information:
1. C3713: 'PropertyChangeListener<T>::propertyChange': an event handler method must have the same function parameters as the source 'Bean::MyEvent'
There is an error in your code. To hook an event to an event receiver function, the signature must be identical in both sides. In your code, the native event is declared as: "__event void MyEvent(std::string value)"; while the event receiver function is declared as: "void propertyChange( std::string oldValue, std::string newValue )" See the function signatures don't match to each other.
2. C3740: 'PropertyChangeListener<T>': templates cannot source or receive events
We don't support templates both in event source and event receiver. You can not hook an event to a member template function. Further more, we might cut Native Event support entirely in the future release. We really encourage you to write your own event codes.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Please feel free to send us any questions.
Thanks for interested in using Microsoft Visual C++ compiler.
Best Regards
Liu Xiong
Mike Sh
I have notified one of the VC team members regarding your post.
Sorry for the delay. Expect a reply soon and thanks for your patience.
Thanks,
Ayman Shoukry
Program Manager
VC++ Team
Bil Simser
This is what I am seeing. I am a newbie. So there could be glaring mistakes in the code. I am attempting to create a common event handling template that event sources can use.
c:\java\c#\Template\PropertyChangeListener.cpp(13) : error C3713: 'PropertyChangeListener<T>::propertyChange': an event handler method must have the same function parameters as the source 'Bean::MyEvent'
with
[
T=Bean
]
c:\java\c#\Template\PropertyChangeListener.cpp(7) : see declaration of 'PropertyChangeListener<T>::propertyChange'
with
[
T=Bean
]
c:\java\c#\Template\PropertyChangeListener.cpp(26) : see declaration of 'Bean::MyEvent'
The event handler is one of:
c:\java\c#\Template\PropertyChangeListener.cpp(7): could be 'void PropertyChangeListener<T>::propertyChange(std::string,std::string)'
with
[
T=Bean
]
The event source is one of:
c:\java\c#\Template\PropertyChangeListener.cpp(26): could be 'void Bean::MyEvent(std::string)'
c:\java\c#\Template\PropertyChangeListener.cpp(12) : while compiling class-template member function 'void PropertyChangeListener<T>::hookEvent(T *)'
with
[
T=Bean
]
c:\java\c#\Template\PropertyChangeListener.cpp(21) : see reference to class template instantiation 'PropertyChangeListener<T>' being compiled
with
[
T=Bean
]
c:\java\c#\Template\PropertyChangeListener.cpp(13) : error C3740: 'PropertyChangeListener<T>': templates cannot source or receive events
with
[
T=Bean
]
Thanks,
Mohan
LeGonzo
Since this entry hasn't received a reply I am going to explain more. I am trying to find out how to separate C# or VC++ code from the event handler. It is a kind of mixin though the source and receiver have a inheritance relationship. I understand that this pattern is called 'Curiously Recurring Template Pattern'
I have been recently smitten by the new language features that C# is going to introduce. So I started to experiment.
Thanks,
Mohan