CLI C++: Events in Interfaces

G

Guest

All -
I am facing couple of problems with events in CLI C++.
If i have an interface that declares few events, and a ref class implements
that interface, should it do anything extra from events perspective?

interface class IMyClass
{
public:
event MyDeleg^ MyEvent;
}

ref class MyClass : IMyClass
{
public:
event MyDeleg^ MyEvent;
}

Compiler says MyClass must provide an implementation of MyEvent::add, which
i presume is explicit event specification, which i dont want to do. (C3766)

Remember that with VS 2003, interface in events tended to be a bit dodgy -
generating warning "C4803". But it used to work.
Is there something extra i am missing out?

And another intersting bit is that, if i dont implement IMyClass in MyClass,
and while i try and raise the event <see code below>

ref class MyClass // No implementation of IMyClass
{
event MyDeleg^ MyEvent;

void Fire_MyEvent()
{
if(MyEvent != nullptr) // *** Error C3918 ***
MyEvent();
}
}

So if we dont do this check for nullness, will the compiler do this bit
automatically for us? If so, that z cool. In VS 2003, guess we used to check
the __event ptr for nullness.

Thanks in advance.

Regardz
Grafix
 
H

Holger Grund

Grafix said:
interface class IMyClass
{
public:
event MyDeleg^ MyEvent;
}

ref class MyClass : IMyClass
{
public:
event MyDeleg^ MyEvent;
}

Compiler says MyClass must provide an implementation of MyEvent::add,
which
i presume is explicit event specification, which i dont want to do.
(C3766)
Looks like a bug to me. If I understand the specification correctly, the
event-definition in MyClass should provide the default implementations for
the
event accessor functions (Not that this makes a whole lot of sense
anyway...)

It looks like you need to add virtual, however.
virtual event MyDeleg^ MyEvent; // trivial event impl
So if we dont do this check for nullness, will the compiler do this bit
automatically for us? If so, that z cool. In VS 2003, guess we used to
check
the __event ptr for nullness.

The compiler does that for you. The implementation vcalls into Invoke only
if the generated delegate field is not nullptr.

-hg
 
Joined
Jan 13, 2010
Messages
1
Reaction score
0
=?Utf-8?B?R3JhZml4?= said:
All -
I am facing couple of problems with events in CLI C++.
If i have an interface that declares few events, and a ref class implements
that interface, should it do anything extra from events perspective?

interface class IMyClass
{
public:
event MyDeleg^ MyEvent;
}

ref class MyClass : IMyClass
{
public:
event MyDeleg^ MyEvent;
}

Compiler says MyClass must provide an implementation of MyEvent::add, which
i presume is explicit event specification, which i dont want to do. (C3766)

Remember that with VS 2003, interface in events tended to be a bit dodgy -
generating warning "C4803". But it used to work.
Is there something extra i am missing out?

And another intersting bit is that, if i dont implement IMyClass in MyClass,
and while i try and raise the event

ref class MyClass // No implementation of IMyClass
{
event MyDeleg^ MyEvent;

void Fire_MyEvent()
{
if(MyEvent != nullptr) // *** Error C3918 ***
MyEvent();
}
}

So if we dont do this check for nullness, will the compiler do this bit
automatically for us? If so, that z cool. In VS 2003, guess we used to check
the __event ptr for nullness.

Thanks in advance.
Regardz
Grafix


its and old post but however here's the solution:

interface class IMyClass
{
public:
event MyDeleg^ MyEvent;
}

ref class MyClass : IMyClass
{
public:
virtual event MyDeleg^ MyEvent;
}

You have to use the virtual keyword (same for properties)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top