Topic: Template and constructor confusion.
Author: Hubert HOLIN <holin@mathp7.jussieu.fr>
Date: 1995/06/28 Raw View
I have typed a short code which behaves as would want it to, but unfortunately, I do
NOT understand why. I should think the code is incomplete, and therefore should not
work properly. Here is the code :
----------- 8< ------ zarbi.cc ----- >8 -----------------
#include <stream.h>
template<class T>
class Doublement
{protected :
T x;
T y;
public :
Doublement(T x0, T y0)
{x=x0;
y=y0;};
Doublement(const Doublement& a_copier)
{x=a_copier.x;
y=a_copier.y;}
Doublement(const long double t[]); // sert a initialiser pratiquement
~Doublement()
{};
Doublement& operator = (const Doublement& a_affecter)
{if( this != &a_affecter )
{x = a_affecter.x;
y = a_affecter.y;}
return( *this );};
friend ostream& operator << (ostream& sortie, const Doublement& w);
};
class f_reel
{private :
long double x;
public :
f_reel(const long double t = 0.0) : x(t) // init. usuelle,
{}; // conversion de type
f_reel(const f_reel& a_copier) // construct. par recopie
{x = a_copier.x;};
f_reel(const long double t[])
{x = t[0];};
~f_reel() // destructeur
{};
f_reel& operator = (const f_reel& a_affecter) // surcharge op. affectation
{if( this != &a_affecter )
{x = a_affecter.x;}
return( *this );};
friend ostream& operator << (ostream& sortie, const f_reel& x);
};
ostream& operator << (ostream& sortie, const f_reel& x)
{sortie << x.x;
return( sortie );}
typedef Doublement<f_reel> f_complexe;
ostream& operator << (ostream& sortie, const f_complexe& z)
{sortie << "( " <<z.x << " ; " << z.y << " )";}
main(int argc,char argv[],char **env)
{cout << "debut du test\n\n";
cout << "initialisation par liste de \"doubles\";\n";
f_reel r((long double [] ){12.345});
cout << "valeur de r (doit etre 12.345) : " << r << "\n\n";
cout << "initialisation par liste de \"doubles\";\n";
f_complexe c((long double [] ){12.345,1.9});
cout << "valeur de c (doit etre (12.345 ; 1.9)) : " << c << "\n\n";
cout << "fin du test.\n\n";}
--------------- 8< -------------- >8 ----------------------
Here is the compilation and execution :
$ gcc -o zarbi zarbi.cc -liostream
$ zarbi
debut du test
initialisation par liste de "doubles";
valeur de r (doit etre 12.345) : 12.345
initialisation par liste de "doubles";
valeur de c (doit etre (12.345 ; 1.9)) : ( 12.345 ; 1.9 )
fin du test.
$
The compiler used is gcc 2.7.0 on a SPARCstation 5.
I do not understand how the initialisation for f_complexe actually works, when I
did not write the code for Doublement(const long double t[]) .
Is this a feature of gcc, or is this a standard behaviour ?
Hubert HOLIN
holin@mathp7.jussieu.fr
Mathematician in exile.