N
Nephi Immortal
Please confirm if template definition is valid. It is the class
body. You may find the missing, but I don’t see the reason to add the
missing since C++ Compiler does not generate an error message. I
should have added A< T, T2 > in each member functions.
If I insert or remove template parameters on top of class, then I do
not need to modify each member functions. The alternative option is
to use typedef.
If I choose to explicit member function definitions outside the class
body, then I have to put template< typename ….> and class<…> on each
memer functions. Typedef is not helpful.
template< typename T, typename T2 >
class A {
public:
A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
}
~A() {
}
A( const A &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
}
A &operator=( const A &right ) {
m_t = right.m_t;
m_t2 = right.m_t2;
return *this;
}
/*
A< T, T2 >( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
}
~A< T, T2 > () {
}
A< T, T2 > ( const A< T, T2 > &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
}
A< T, T2 > &operator=( const A< T, T2 > &right ) {
m_t = right.m_t;
m_t2 = right.m_t2;
return *this;
}
*/
private:
T m_t;
T2 m_t2;
};
int main() {
A< char, int > a( 1, 2 ), a2( 3, 4 );
a = a2;
return 0;
}
body. You may find the missing, but I don’t see the reason to add the
missing since C++ Compiler does not generate an error message. I
should have added A< T, T2 > in each member functions.
If I insert or remove template parameters on top of class, then I do
not need to modify each member functions. The alternative option is
to use typedef.
If I choose to explicit member function definitions outside the class
body, then I have to put template< typename ….> and class<…> on each
memer functions. Typedef is not helpful.
template< typename T, typename T2 >
class A {
public:
A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
}
~A() {
}
A( const A &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
}
A &operator=( const A &right ) {
m_t = right.m_t;
m_t2 = right.m_t2;
return *this;
}
/*
A< T, T2 >( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
}
~A< T, T2 > () {
}
A< T, T2 > ( const A< T, T2 > &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
}
A< T, T2 > &operator=( const A< T, T2 > &right ) {
m_t = right.m_t;
m_t2 = right.m_t2;
return *this;
}
*/
private:
T m_t;
T2 m_t2;
};
int main() {
A< char, int > a( 1, 2 ), a2( 3, 4 );
a = a2;
return 0;
}