M
ma740988
Part of my confusion here is certainly my ignorance of templates and
std::auto_ptr. Two topics, I've perused but need to really _delve_
into. In any event, consider the 'test' source.
# include <iostream>
# include <memory>
class CallbackBase
{
public:
virtual void operator()() const { };
virtual ~CallbackBase() = 0;
};
CallbackBase::~CallbackBase() { }
template<typename T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)();
Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()() const { (t_->*f_)(); }
private:
T* t_;
F f_;
};
template<typename T>
Callback<T> make_callback( T& t, void (T::*f) () ) {
return Callback<T>( t, f );
}
template<class T>
std::auto_ptr<CallbackBase> new_callback( T& t, void(T::*f)() ) {
return std::auto_ptr<CallbackBase>( new Callback<T>(t,f) ); }
class XYZ {
std::auto_ptr<CallbackBase> my_cb;
static void cb_test()
{
// call the user function passed in to the
// constructor of the sndr & slComm.
//CallbackBase& cb = *static_cast<CallbackBase*>(&my_cb.get());
//cb();
}
public:
template<typename T>
XYZ( T& t,
void(T::*f)() )
: my_cb( new_callback(t,f) )
{}
};
class use {
XYZ *x;
public:
use()
{
x = new XYZ(this, use::test_funct); // DOESN'T WORK
}
void test_funct() {
std::cout << " test_func called " << std::endl;
}
~use () { delete x; }
};
int main()
{
//use u;
}
1 ////
I'm trying to create an instance of the class XYZ within 'use'
constructor but the .NET 2003 compiler complains, that the constructor
for XYZ does _not_ take 2 parameters.
'XYZ::XYZ' : function does not take 2 parameters
Make no sense.
2////
Within the cb_test member function whats the right approach to calling
the void function passed into XYZ?
The commented out portion. i.e.
//CallbackBase& cb = *static_cast<CallbackBase*>(&my_cb.get());
//cb();
doesn't work
std::auto_ptr. Two topics, I've perused but need to really _delve_
into. In any event, consider the 'test' source.
# include <iostream>
# include <memory>
class CallbackBase
{
public:
virtual void operator()() const { };
virtual ~CallbackBase() = 0;
};
CallbackBase::~CallbackBase() { }
template<typename T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)();
Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()() const { (t_->*f_)(); }
private:
T* t_;
F f_;
};
template<typename T>
Callback<T> make_callback( T& t, void (T::*f) () ) {
return Callback<T>( t, f );
}
template<class T>
std::auto_ptr<CallbackBase> new_callback( T& t, void(T::*f)() ) {
return std::auto_ptr<CallbackBase>( new Callback<T>(t,f) ); }
class XYZ {
std::auto_ptr<CallbackBase> my_cb;
static void cb_test()
{
// call the user function passed in to the
// constructor of the sndr & slComm.
//CallbackBase& cb = *static_cast<CallbackBase*>(&my_cb.get());
//cb();
}
public:
template<typename T>
XYZ( T& t,
void(T::*f)() )
: my_cb( new_callback(t,f) )
{}
};
class use {
XYZ *x;
public:
use()
{
x = new XYZ(this, use::test_funct); // DOESN'T WORK
}
void test_funct() {
std::cout << " test_func called " << std::endl;
}
~use () { delete x; }
};
int main()
{
//use u;
}
1 ////
I'm trying to create an instance of the class XYZ within 'use'
constructor but the .NET 2003 compiler complains, that the constructor
for XYZ does _not_ take 2 parameters.
'XYZ::XYZ' : function does not take 2 parameters
Make no sense.
2////
Within the cb_test member function whats the right approach to calling
the void function passed into XYZ?
The commented out portion. i.e.
//CallbackBase& cb = *static_cast<CallbackBase*>(&my_cb.get());
//cb();
doesn't work