?
=?iso-8859-1?B?UOVobCBNZWxpbg==?=
I have some problems using conversion operators in C++/CLI. In my
project I have two ref class:es Signal and SignalMask and I have an
conversion function in Signal to convert Signal:s to SignalMask:s. The
reason is I have a free function called WaitSignal that accepts av
SignalMask where Signals parameters are supposed to implicitly be
converted to SignalMask:s. I'm using the SignalMask class because I
want to be able to supply a logic expression of which signals I want to
listen to at the same time:
WaitSignal(s1 || s2 || s3); // wait for any of the three signals
But to make the function WaitSignal accept a single Signal argument I'm
relying on the conversion operator in the Signal class to convert a
Signal argument to a SignalMask. As in normal C++ this requires a const
reference to work since it's not allowed to call a function with a
non-const temporary object.
That's fine. But the problem is that in C++/CLI you are not allowed to
use a const object in *any* way since you cannot specify which
operations on a class is const and which are not.
So I'm required to supply a const object reference to call the
conversion operator but then I cannot use the object! I'm I missing
something or is this a known problem in C++/CLI?
Note that I'm using the ref classes with stack semantic to
deteministically destruct the objects.
A simple example:
=================
ref class SignalMask;
ref class Signal {
public:
operator SignalMask();
};
ref class SignalMask {
public:
SignalMask(SignalMask% other);
void DoAnything() { }
};
void WaitSignal(const SignalMask% mask)
{
// *any* use of object mask is forbidden
// - even const operations (which you can't specify)
mask.DoAnything();
}
int main()
{
Signal s1;
WaitSignal(s1);
return 0;
}
If I remove the const qualifier in WaitSignal(const SignalMask% mask)
the conversion operator is not invoked and if I keep const I cannot use
the object supplied. Catch 22!
Thanks in advance.
Best Regards,
Påhl
project I have two ref class:es Signal and SignalMask and I have an
conversion function in Signal to convert Signal:s to SignalMask:s. The
reason is I have a free function called WaitSignal that accepts av
SignalMask where Signals parameters are supposed to implicitly be
converted to SignalMask:s. I'm using the SignalMask class because I
want to be able to supply a logic expression of which signals I want to
listen to at the same time:
WaitSignal(s1 || s2 || s3); // wait for any of the three signals
But to make the function WaitSignal accept a single Signal argument I'm
relying on the conversion operator in the Signal class to convert a
Signal argument to a SignalMask. As in normal C++ this requires a const
reference to work since it's not allowed to call a function with a
non-const temporary object.
That's fine. But the problem is that in C++/CLI you are not allowed to
use a const object in *any* way since you cannot specify which
operations on a class is const and which are not.
So I'm required to supply a const object reference to call the
conversion operator but then I cannot use the object! I'm I missing
something or is this a known problem in C++/CLI?
Note that I'm using the ref classes with stack semantic to
deteministically destruct the objects.
A simple example:
=================
ref class SignalMask;
ref class Signal {
public:
operator SignalMask();
};
ref class SignalMask {
public:
SignalMask(SignalMask% other);
void DoAnything() { }
};
void WaitSignal(const SignalMask% mask)
{
// *any* use of object mask is forbidden
// - even const operations (which you can't specify)
mask.DoAnything();
}
int main()
{
Signal s1;
WaitSignal(s1);
return 0;
}
If I remove the const qualifier in WaitSignal(const SignalMask% mask)
the conversion operator is not invoked and if I keep const I cannot use
the object supplied. Catch 22!
Thanks in advance.
Best Regards,
Påhl