about class specialization in templates

J

jesse

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
 
K

klaas

jesse said:
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?
 
R

Rob Williscroft

jesse wrote in
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 said:
{

}

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.
 
J

Jerry Coffin

[email protected] says... said:
//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.
 
J

jesse

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
 
J

jesse

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
 
J

jesse

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top