J
Jeroen
Hi guys,
I have a simple question. If I have a class like:
class A {
A();
~A();
A(A& a);
A(int i);
int myvalue;
};
So there's a copy constructor for integers:
A::A(int i) { myvalue = i;}
But if I also want copy constructors for unsigned int, long, float,
double, long double etc, do I have to write them all out? All bodies of
these copy constructors are exactly the same:
A::A(T& t) { myvalue = t;}
Or can templates help me without having to write out all variants?
And what if I have to deal with other classes/types which are not
clearly a 'simple scalar'? For example 'complex'? This needs another
code body because then I need to get the real value (for example):
A::A(complex<double>& c) { myvalue = c.real();}
And of course the complex variable comes in various variants like
double, float, etc...
Thanks for any hints,
Jeroen
I have a simple question. If I have a class like:
class A {
A();
~A();
A(A& a);
A(int i);
int myvalue;
};
So there's a copy constructor for integers:
A::A(int i) { myvalue = i;}
But if I also want copy constructors for unsigned int, long, float,
double, long double etc, do I have to write them all out? All bodies of
these copy constructors are exactly the same:
A::A(T& t) { myvalue = t;}
Or can templates help me without having to write out all variants?
And what if I have to deal with other classes/types which are not
clearly a 'simple scalar'? For example 'complex'? This needs another
code body because then I need to get the real value (for example):
A::A(complex<double>& c) { myvalue = c.real();}
And of course the complex variable comes in various variants like
double, float, etc...
Thanks for any hints,
Jeroen