By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

How to Init static variable in Template

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"naruto" <na*********@hotmail.com> wrote in message news:e0**************************@posting.google.c om...
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;}
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

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #2

P: n/a
Naruto,
What exactly are you trying to achieve with this template?
I am getting an error T is not a namespace or class name.
"naruto" <na*********@hotmail.com> wrote in message
news:e0**************************@posting.google.c om...
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

Jul 22 '05 #3

P: n/a
"Alex Vinokur" <al****@big.foot.com> wrote in message news:<2h************@uni-berlin.de>...
"naruto" <na*********@hotmail.com> wrote in message news:e0**************************@posting.google.c om...
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;}
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


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

Thanks,
naruto
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.