How to Init static variable in Template

N

naruto

Hi all,

I got an compile error saying "static variables should be init. at
point of declaration" when I try to the following:

// test.h
template<typename T>
class Callback
{
public:
typedef void (T::*F)();

Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()() const { (t_->*f_)(); }

static c_func() { (t_->*f_); }

private:

static T* t_;
F f_;
};

// test.cxx
#include "test.h"

template<typename T>
T* Callback<T>::t_ = 0;

int main()
{
}

Is there a way to fix this? I have a function that only takes in
function pointer and I would like to create a c_func() to simulate
this.

Thanks,
Naruto
 
A

Alex Vinokur

naruto said:
Hi all,

I got an compile error saying "static variables should be init. at
point of declaration" when I try to the following:

// test.h
template<typename T>
class Callback
{
public:
typedef void (T::*F)();

Callback( T& t, F f ) : t_(&t), f_(f) { }
Callback( T& t, F f ) : f_(f) { t_ = &t;}
 
K

Kench

Naruto,
What exactly are you trying to achieve with this template?
I am getting an error T is not a namespace or class name.
 
N

naruto

Alex Vinokur said:
Callback( T& t, F f ) : f_(f) { t_ = &t;}

Thanks .... that solves the problem. So, the compiler error
complaints the initialization list.

Thanks,
naruto
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top