473,383 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to Init static variable in Template

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
3 3171

"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Graham Dumpleton | last post by:
When you have a template class with a static member variable, ie., template<typename T> class handle { public: static void* id() { return &id_; } private: static int id_; };
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
4
by: santosh | last post by:
Hello, I have a doubt about static variable. class B { private: static int x; public: static set(int y) {
3
by: paulw | last post by:
Hi I have a question: main() { static int i; printf("%d\n",i); // should I see see 0 or 5 ??? for (i=5;i<=15;i++) {...} // What's the meaning of static variable in
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
2
by: Christof Warlich | last post by:
Hi, I'm working on a (template) library that is up to now entirely implemented in header-files. This makes the library quite convenient to use as no extra object code needs to be linked when...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.