S
Sebastian Faust
Hi,
I have 4 questions related to templates.
I wanna do something like the following:
template<typename T>
class Template
{
public:
Template_Test<T>()
{
j=999;
}
public:
int j;
};
class Tester
{
public:
std::list<class Template> List;
template<typename T>
void update(Template<T>& t)
{
int i = t.j;
}
};
Now my questions:
1.) In the class Tester I wanna use as the template parameter of the
std::list-class the class Template<>. I get an error at this position at
compile time ( VC.Net ). I know I could make the class Tester a class
template too and then do the following: std::list<T>, but the problem is
that I dont wanna make the Tester class a class template. So is there a
possibility how to tell the class template std::list that is should use as
template parameter a class template of Template?
2.) I made the update() member function a function template, but what I
really wanna here is only that it uses as argument the class template
Template. Is there a possibilty to do such without making update a function
template?
3.) Is it possible to use a class template as argument for a constructor,
without making the class ( in which the constructor is declared ) a class
template?
I mean something like this:
class constructor
{
constructor(Template& t) { }
};
4.) Does it make any sense to implement the observer-pattern with templates?
template<typename T>
class Subject
{
std::list<T> observers;
public:
void attach(T observer);
void detach(T observer);
};
template<typename T>
class observer
{
void update(T subject);
};
If it is possible then is my idea correct?
My problem is that at one point I wanna make a break in template using, that
means for example that the class which holds the subjects shouldnt be a
template class. But which subjects are used is only known at runtime and the
different subjects will be created at runtime, so only then it will be known
which subjects are connected with which observers. Maybe at this point using
template doesnt make any sense. Sadly I am still not that familar with
templates, so for some advice I would be very glad.
Cause I bought the book C++ templates from Josuttis and Vandevoorde, maybe
someone can tell me where in the book I can find some information about my
questions. Sadly I only read till page 60 ( I got the book some days ago,
and it's really well written ) and I wanna now use some template things in
my actual project. Sadly I couldnt find the information about my topics in
the index.
For any help thanks a lot in advance
Sebastian
I have 4 questions related to templates.
I wanna do something like the following:
template<typename T>
class Template
{
public:
Template_Test<T>()
{
j=999;
}
public:
int j;
};
class Tester
{
public:
std::list<class Template> List;
template<typename T>
void update(Template<T>& t)
{
int i = t.j;
}
};
Now my questions:
1.) In the class Tester I wanna use as the template parameter of the
std::list-class the class Template<>. I get an error at this position at
compile time ( VC.Net ). I know I could make the class Tester a class
template too and then do the following: std::list<T>, but the problem is
that I dont wanna make the Tester class a class template. So is there a
possibility how to tell the class template std::list that is should use as
template parameter a class template of Template?
2.) I made the update() member function a function template, but what I
really wanna here is only that it uses as argument the class template
Template. Is there a possibilty to do such without making update a function
template?
3.) Is it possible to use a class template as argument for a constructor,
without making the class ( in which the constructor is declared ) a class
template?
I mean something like this:
class constructor
{
constructor(Template& t) { }
};
4.) Does it make any sense to implement the observer-pattern with templates?
template<typename T>
class Subject
{
std::list<T> observers;
public:
void attach(T observer);
void detach(T observer);
};
template<typename T>
class observer
{
void update(T subject);
};
If it is possible then is my idea correct?
My problem is that at one point I wanna make a break in template using, that
means for example that the class which holds the subjects shouldnt be a
template class. But which subjects are used is only known at runtime and the
different subjects will be created at runtime, so only then it will be known
which subjects are connected with which observers. Maybe at this point using
template doesnt make any sense. Sadly I am still not that familar with
templates, so for some advice I would be very glad.
Cause I bought the book C++ templates from Josuttis and Vandevoorde, maybe
someone can tell me where in the book I can find some information about my
questions. Sadly I only read till page 60 ( I got the book some days ago,
and it's really well written ) and I wanna now use some template things in
my actual project. Sadly I couldnt find the information about my topics in
the index.
For any help thanks a lot in advance
Sebastian