 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sebastian Faust Guest
|
Posted: Sat Oct 18, 2003 10:09 pm Post subject: template parameters in non-template class |
|
|
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sun Oct 19, 2003 8:38 am Post subject: Re: template parameters in non-template class |
|
|
"Sebastian Faust" <sfaust_deleteme (AT) logic-software (DOT) de> wrote
| Quote: | 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?
|
Well you haven't really explained what it is you are trying to do.
You can't overload data objects, so the class really needs to know
what the type of List is. How is the compiler supposed to guess
what type to use.
| Quote: | 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?
|
This is sounding more and more like Tester a template. Why do you resist
other than "don't wanna?"
| Quote: | 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) { }
};
|
You can template constructors. They work like any other member function in
this regard.
template <class T> constructor(Template<T>& t) { }
if I understand you correctly.
| Quote: | 4.) Does it make any sense to implement the observer-pattern with templates?
template<typename T
class Subject
{
std::list
public:
void attach(T observer);
void detach(T observer);
};
template
class observer
{
void update(T subject);
}
This is ok, I guess other than it makes things confusing as there is no bound |
on what type can be fed to your subject/observer keys. Really, deriving from
a common observer base class and using virtual functions makes more polymorphic
sense.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Edward Diener Guest
|
Posted: Sun Oct 19, 2003 8:47 am Post subject: Re: template parameters in non-template class |
|
|
Sebastian Faust wrote:
| Quote: | Hi,
I have 4 questions related to templates.
I wanna do something like the following:
template<typename T
class Template
|
class Template_Test, n'est-ce-pas ?
| Quote: | {
public:
Template_Test
{
j=999;
}
public:
int j;
};
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sun Oct 19, 2003 4:10 pm Post subject: Re: template parameters in non-template class |
|
|
Sebastian Faust wrote:
| Quote: | I wanna do something like the following:
template<typename T
class Template
{
public:
Template_Test
|
This won't compile. Looks like a ctor, but Template_Test isn't the
classname and for a function it lacks the return-type. Remember: minimal
compilable code. Also, your code is really hard to read due to the mixture
of 'template' and 'Template'.
| Quote: | {
j=999;
}
public:
int j;
};
class Tester
{
public:
std::list<class Template> List;
|
1. the 'class' or 'struct' in the template-parameter list is not necessary,
but I'm also not sure if it is definitely wrong.
2. 'Template' is not a type. It is also not a class(!) but a class
template. Only by using it like e.g. Template<string> it becomes a type.
std::list<> needs a type as parameter.
| Quote: | 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?
|
Maybe you want runtime polymorphism (base-class with virtual functions)
instead of compile-time polymorphism (templates). What I would definitely
take a look at is a signal-library like [1] or [2].
Uli
[1] libsigC++: http://libsigc.sf.net
[2] Boost signals: http://boost.org
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sebastian Faust Guest
|
Posted: Mon Oct 20, 2003 9:48 pm Post subject: Re: template parameters in non-template class |
|
|
Hi,
| Quote: | 4.) Does it make any sense to implement the observer-pattern with
templates?
template<typename T
class Subject
{
std::list
public:
void attach(T observer);
void detach(T observer);
};
template
class observer
{
void update(T subject);
}
This is ok, I guess other than it makes things confusing as there is no
bound
on what type can be fed to your subject/observer keys. Really, deriving
from
a common observer base class and using virtual functions makes more
polymorphic
sense.
So you would give me the advice to use in my situation ( observer-pattern ) |
a common observer base class with virtual functions instead of templates? Is
there any reason why this is better in this case?
Bye,
Sebastian
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|