I am frustrated by class specialization. i don't
think it helps me a lot.
suppose we have
template <class T>
class Talkative
{
T& t;
public:
Talkative( T& obj ) :
t( obj ) {
};
~Talkative(){};
void talk(){t.talk();};
void sing(){t.sing();};
void whisper(){t.whisper();};
};
//through class specialization , i want:
// 1) inherit the sing() from the base template class;
// 2) specialization the talk();
// 3) and add a new function bark();
template <>
class Talkative<Dog>
{
}
then in the declaration of
template <> class Talkative<Dog>, i need to re-write
everthing(code) that are already in the templates.
from constructor, destructor, copy-structor,
sing(), class variables t.
what a pity? is there any way to avoid this?
i was so frustrated by this issue!
(class member specialization doesn't fit here, since
i need to add a new function bark(); )
If you have good thoughts, could you please share with me.
thanks!
jesse 6 2509
jesse wrote: I am frustrated by class specialization. i don't think it helps me a lot.
suppose we have
template <class T> class Talkative { T& t; public: Talkative( T& obj ) : t( obj ) { }; ~Talkative(){};
void talk(){t.talk();}; void sing(){t.sing();}; void whisper(){t.whisper();}; };
//through class specialization , i want: // 1) inherit the sing() from the base template class; // 2) specialization the talk(); // 3) and add a new function bark();
template <> class Talkative<Dog> {
}
then in the declaration of template <> class Talkative<Dog>, i need to re-write everthing(code) that are already in the templates. from constructor, destructor, copy-structor, sing(), class variables t.
what a pity? is there any way to avoid this? i was so frustrated by this issue!
(class member specialization doesn't fit here, since i need to add a new function bark(); )
If you have good thoughts, could you please share with me. thanks!
jesse
class x:public Talkative<ass>
{
/*constructors*/
void bark() {/*whatever*/}
void sing() {Talkative::sing()}
}
why do you need specialization?
jesse wrote in news:3F************@yahoo.com: I am frustrated by class specialization. i don't think it helps me a lot.
suppose we have
template <class T> class Talkative
class Talkative_base
{ T& t; public: Talkative( T& obj ) : t( obj ) { }; ~Talkative(){};
void talk(){t.talk();}; void sing(){t.sing();}; void whisper(){t.whisper();}; };
//through class specialization , i want: // 1) inherit the sing() from the base template class; // 2) specialization the talk(); // 3) and add a new function bark();
template < typename T >
class Talkative: public Talkative_base< T >
{
};
template <> class Talkative<Dog>
class Talkative<Dog> : public Talkative_base< Dog >
{
}
then in the declaration of template <> class Talkative<Dog>, i need to re-write everthing(code) that are already in the templates. from constructor, destructor, copy-structor, sing(), class variables t.
what a pity? is there any way to avoid this? i was so frustrated by this issue!
(class member specialization doesn't fit here, since i need to add a new function bark(); )
If you have good thoughts, could you please share with me. thanks!
Don't mix specialization and inheritance, they are not (and AFAIKT
were never meant to be) the same thing.
HTH
Rob.
-- http://www.victim-prime.dsl.pipex.com/
In article <3F************@yahoo.com>, je*****@yahoo.com says...
[ ... ] //through class specialization , i want: // 1) inherit the sing() from the base template class; // 2) specialization the talk(); // 3) and add a new function bark();
Specialization gives specialization, not inheritance. If you want
inheritance, that's what you should use.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Rob & all:
thanks for your all advice.
That is the key point: I tried to mix specialization and inheritance.
it is impossible to do it in one step. it has to be done in two steps,
jesse
Rob Williscroft wrote: jesse wrote in news:3F************@yahoo.com:
I am frustrated by class specialization. i don't think it helps me a lot.
suppose we have
template <class T> class Talkative
class Talkative_base
{ T& t; public: Talkative( T& obj ) : t( obj ) { }; ~Talkative(){};
void talk(){t.talk();}; void sing(){t.sing();}; void whisper(){t.whisper();}; };
//through class specialization , i want: // 1) inherit the sing() from the base template class; // 2) specialization the talk(); // 3) and add a new function bark();
template < typename T > class Talkative: public Talkative_base< T > { }; template <> class Talkative<Dog> class Talkative<Dog> : public Talkative_base< Dog >
{
}
then in the declaration of template <> class Talkative<Dog>, i need to re-write everthing(code) that are already in the templates. from constructor, destructor, copy-structor, sing(), class variables t.
what a pity? is there any way to avoid this? i was so frustrated by this issue!
(class member specialization doesn't fit here, since i need to add a new function bark(); )
If you have good thoughts, could you please share with me. thanks!
Don't mix specialization and inheritance, they are not (and AFAIKT were never meant to be) the same thing.
HTH
Rob.
Rob & all:
thanks for your all advice.
That is the key point: I tried to mix specialization and inheritance.
it is impossible to do it in one step. it has to be done in two steps,
jesse
Rob Williscroft wrote: jesse wrote in news:3F************@yahoo.com:
I am frustrated by class specialization. i don't think it helps me a lot.
suppose we have
template <class T> class Talkative
class Talkative_base
{ T& t; public: Talkative( T& obj ) : t( obj ) { }; ~Talkative(){};
void talk(){t.talk();}; void sing(){t.sing();}; void whisper(){t.whisper();}; };
//through class specialization , i want: // 1) inherit the sing() from the base template class; // 2) specialization the talk(); // 3) and add a new function bark();
template < typename T > class Talkative: public Talkative_base< T > { }; template <> class Talkative<Dog> class Talkative<Dog> : public Talkative_base< Dog >
{
}
then in the declaration of template <> class Talkative<Dog>, i need to re-write everthing(code) that are already in the templates. from constructor, destructor, copy-structor, sing(), class variables t.
what a pity? is there any way to avoid this? i was so frustrated by this issue!
(class member specialization doesn't fit here, since i need to add a new function bark(); )
If you have good thoughts, could you please share with me. thanks!
Don't mix specialization and inheritance, they are not (and AFAIKT were never meant to be) the same thing.
HTH
Rob.
Rob and all:
thanks for yor all advice. the point here is i tried to mix
inheritance and specialization.
It has to be done in two steps, not in one step. as Ron suggested.
the only overhead is that i need to rewrite constructor.
In com.lang.c++.moderate newsgroup, Aaron Bentley comes with another
good idea:
template <class T, int specialize=true>
class Talkative
{
T& t;
public:
Talkative( T& obj ) :
t( obj ) {
};
~Talkative(){};
void talk(){t.talk();};
void sing(){t.sing();};
void whisper(){t.whisper();};
};
template <> class Talkative<Dog, true>: public Talkative<Dog, false>
{
public:
bark() {}
};
this idea also needs rewrite constructor.
jesse
Rob Williscroft wrote: jesse wrote in news:3F************@yahoo.com:
I am frustrated by class specialization. i don't think it helps me a lot.
suppose we have
template <class T> class Talkative
class Talkative_base
{ T& t; public: Talkative( T& obj ) : t( obj ) { }; ~Talkative(){};
void talk(){t.talk();}; void sing(){t.sing();}; void whisper(){t.whisper();}; };
//through class specialization , i want: // 1) inherit the sing() from the base template class; // 2) specialization the talk(); // 3) and add a new function bark();
template < typename T > class Talkative: public Talkative_base< T > { }; template <> class Talkative<Dog> class Talkative<Dog> : public Talkative_base< Dog >
{
}
then in the declaration of template <> class Talkative<Dog>, i need to re-write everthing(code) that are already in the templates. from constructor, destructor, copy-structor, sing(), class variables t.
what a pity? is there any way to avoid this? i was so frustrated by this issue!
(class member specialization doesn't fit here, since i need to add a new function bark(); )
If you have good thoughts, could you please share with me. thanks!
Don't mix specialization and inheritance, they are not (and AFAIKT were never meant to be) the same thing.
HTH
Rob. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by Jeff |
last post: by
|
4 posts
views
Thread by SainTiss |
last post: by
|
1 post
views
Thread by SainTiss |
last post: by
|
1 post
views
Thread by Alfonso Morra |
last post: by
|
6 posts
views
Thread by wkaras |
last post: by
|
4 posts
views
Thread by Joseph Turian |
last post: by
|
75 posts
views
Thread by Steven T. Hatton |
last post: by
|
7 posts
views
Thread by (2b|!2b)==? |
last post: by
| | | | | | | | | | | |