I
Immortal Nephi
Sometimes, I want to create two different objects. Two different
objects should always have same data member's name. If second object
assigns different data member's name, then C++ Compiler will generate
error saying no such data member's name is found.
My question is -- should I always declare class name or use
template? How do I write template correctly?
class Test
{
public:
Test() : a( 0 ), b( 0 ) {}
~Test() {}
int a;
int b;
};
class Test2
{
public:
Test2() : c( 0 ), d( 0 ) {}
~Test2() {}
int c;
int d;
};
template< class T >
void Modify( T t )
{
t.a++;
t.b++;
}
int main()
{
Test T;
Test2 T2;
Modify( T );
Modify( T2 ); // Error data member c and d do not exist.
return 0;
};
objects should always have same data member's name. If second object
assigns different data member's name, then C++ Compiler will generate
error saying no such data member's name is found.
My question is -- should I always declare class name or use
template? How do I write template correctly?
class Test
{
public:
Test() : a( 0 ), b( 0 ) {}
~Test() {}
int a;
int b;
};
class Test2
{
public:
Test2() : c( 0 ), d( 0 ) {}
~Test2() {}
int c;
int d;
};
template< class T >
void Modify( T t )
{
t.a++;
t.b++;
}
int main()
{
Test T;
Test2 T2;
Modify( T );
Modify( T2 ); // Error data member c and d do not exist.
return 0;
};