468,738 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

about class specialization in templates

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

Jul 19 '05 #1
6 2459
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?

Jul 19 '05 #2
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/
Jul 19 '05 #3
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.
Jul 19 '05 #4
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.


Jul 19 '05 #5
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.


Jul 19 '05 #6
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.


Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by wkaras | last post: by
75 posts views Thread by Steven T. Hatton | last post: by
7 posts views Thread by (2b|!2b)==? | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.