C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template et operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
JBB
Guest





PostPosted: Mon Oct 24, 2005 3:09 pm    Post subject: template et operator Reply with quote



J'ai une classe template representant une chaine de caractere.
(je n'ai pas le droit d'utiliser string)


template <unsigned int N>
class FixString
{

char texte[N+1];
};

j'ai une autre classe me permettant de depiler des trames:

class Trame
{
operator >> (int &);
operator >> (short &);

};

Le but etant de faire: (mon protocole envoi les chiffres sous forme
binaire (accessoirement bigendian) et les textes avec un 0 à la fin)

....
Trame trame;

int champ1;
short champ2;
FixString<10> champ3;
FixString<5> champ4;

trame >> champ1;
trame >> champ2;
trame >> champ3;
trame >> champ4;
....

Mon problème est donc:
comment je defini un operateur >> qui prenne un template comme type
d'operande.

J'imagine que je peux faire:
class Trame
{
operator >> (int &);
operator >> (short &);
operator >> (FixString<10>);
operator >> (FixString<5>);
...
} mais c'est pas super générique

Ne pourrais-je pas plutôt faire
class Trame
{
operator >> (int &);
operator >> (short &);
operator >> (FixString<int N>);
}
Back to top
Fabien LE LEZ
Guest





PostPosted: Mon Oct 24, 2005 3:16 pm    Post subject: Re: template et operator Reply with quote



On Mon, 24 Oct 2005 17:09:05 +0200, JBB <merci (AT) pasdespam (DOT) fr>:

Quote:
class Trame
{
template <int N> Trame& operator >> (FixString<N>&);




Back to top
JBB
Guest





PostPosted: Wed Oct 26, 2005 9:54 am    Post subject: Re: template et operator Reply with quote



Fabien LE LEZ wrote:
Quote:
On Mon, 24 Oct 2005 17:09:05 +0200, JBB <merci (AT) pasdespam (DOT) fr>:

class Trame
{
template <int N> Trame& operator >> (FixString<N>&);


Et est possible de faire une methode template qui ne change qu'en

fonction du type de retour:

ex:
class FixString
{

char texte[N+1];
};



class Trame
{
template<int N>
(FixString<N>)f();

};

J'ai bien peur que non...

Back to top
Fabien LE LEZ
Guest





PostPosted: Wed Oct 26, 2005 9:59 am    Post subject: Re: template et operator Reply with quote

On Wed, 26 Oct 2005 11:54:55 +0200, JBB <merci (AT) pasdespam (DOT) fr>:

Quote:
class Trame
{
template<int N
(FixString

Essaie ça :

template <int N> FixString<N> f();

Par contre, bien évidemment, il faudra spécifier explicitement le
paramètre template lors de l'appel de la fonction.


Back to top
JBB
Guest





PostPosted: Fri Oct 28, 2005 8:21 am    Post subject: Re: template et operator Reply with quote

Fabien LE LEZ wrote:
Quote:
On Wed, 26 Oct 2005 11:54:55 +0200, JBB <merci (AT) pasdespam (DOT) fr>:

class Trame
{
template<int N
(FixString
Essaie ça :

template <int N> FixString<N> f();

Par contre, bien évidemment, il faudra spécifier explicitement le
paramètre template lors de l'appel de la fonction.

A noter qu'il semble que cela ne fonctionne pas avec Visual C++ 6.

J'obtiens une erreur de compilation sur la ligne:

Trame t;
FixString<5> s = t.f<5>();

Par contre cela fonctionne avec Visual Studio .2003. Et avec ma version
de gcc (4.0.0).

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.