M
ma740988
Consider the source snippet.
class foo_base
{};
template <typename T>
class foo : public foo_base
{
public:
typedef void (T::*F)();
foo( T& t, F f) : t_(&t), f_(f) {}
void operator()() const
{
(t_->*f_)();
}
private:
T* t_;
F f_;
};
template <class T>
std::auto_ptr<foo_base> new_cb(T& t, void (T::*f)())
{
return std::auto_ptr<foo_base>(new foo<T>(t, f));
}
---------
class my_class {
static std::auto_ptr<foo_base> test;
public:
template <typename T>
my_class ( T& t, void (T::*f)()) {
test = new_cb(t, f); /// 2
}
};
---------
The line indicated by the number '2' works on the .NET 03 compiler but
gcc complains about initilization of non-constant reference.
C:\SW\MJT\Test\test.h:200: initialization of non-const reference
type `class auto_ptr<foo_base> &'
C:\SW\MJT\Test\test.h:200: from rvalue of type `auto_ptr<foo_base>'
'long winded directory deleted'
include\g++-3\memory:40: in passing argument 1 of
`auto_ptr<foo_base>:
perator =(auto_ptr<foo_base> &)'
Not sure if i follow the error and what the solution is. Thanks in
advance.
class foo_base
{};
template <typename T>
class foo : public foo_base
{
public:
typedef void (T::*F)();
foo( T& t, F f) : t_(&t), f_(f) {}
void operator()() const
{
(t_->*f_)();
}
private:
T* t_;
F f_;
};
template <class T>
std::auto_ptr<foo_base> new_cb(T& t, void (T::*f)())
{
return std::auto_ptr<foo_base>(new foo<T>(t, f));
}
---------
class my_class {
static std::auto_ptr<foo_base> test;
public:
template <typename T>
my_class ( T& t, void (T::*f)()) {
test = new_cb(t, f); /// 2
}
};
---------
The line indicated by the number '2' works on the .NET 03 compiler but
gcc complains about initilization of non-constant reference.
C:\SW\MJT\Test\test.h:200: initialization of non-const reference
type `class auto_ptr<foo_base> &'
C:\SW\MJT\Test\test.h:200: from rvalue of type `auto_ptr<foo_base>'
'long winded directory deleted'
include\g++-3\memory:40: in passing argument 1 of
`auto_ptr<foo_base>:
Not sure if i follow the error and what the solution is. Thanks in
advance.