How to use template's data member?

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;
};
 
L

LR

Immortal said:
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?

I'm not sure I understood that.
How do I write template correctly?

Perhaps like this:

If you can modify Test and Test2
class Test
{
public:
Test() : a( 0 ), b( 0 ) {}
~Test() {}

int &q() { return a; }
int &r() { return b; }
int a;
int b;
};

class Test2
{
public:
Test2() : c( 0 ), d( 0 ) {}
~Test2() {}

int &q() { return c; }
int &r() { return d; }
int c;
int d;
};

template< class T >
// > void Modify( T t )
// void Modify( T t ) won't work.
void Modify(T &t) {
t.q()++;
t.r()++;
}
If you can't modify Test and Test1 then you can try to make helper
structs that get the same info perhaps like this:

struct TestHelper {... similar to Test2Helper ... };

struct Test2Helper {
Test2 &t;
Test2Helper(Test2 &ta) : t(ta) {}
int &q() { return t.c; }
int &r() { return t.d; }
};

template<typename T>
void modifyB(T &t) {
t.q()++;
t.r()++;
}

You can use modifyB the same way you used modify in main,
modify(T);
modify(T2);



It's not clear from your question, but suppose you have a whole bunch of
classes that are similar to Test, ie, they have variable a and b, and
only a few that are different line Test2, ie they don't have variable a
and b. The you may want to use explicit specialization. You can use
explicit specialization even if you only have a few classes.


// this template will get all the classes that have
// variables a and b.
template<typename T>
void modifyX(T &t) {
t.a++;
t.b++;
}

// specialize classes like Test2 that don't have a and b.
template<>
void modifyX<Test2>(Test2 &t) {
t.c++;
t.d++;
}

There are other ways depending on how many classes you have and what the
distribution of variable names looks like.

LR
 
Ö

Öö Tiib

        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() {}
delete these:
        int c;
        int d;

replace with these:

long a;
long b;
};

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.

should work now. ;)
 
S

Saeed Amrollahi

        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;



};- Hide quoted text -

- Show quoted text -

Hi

If you access to Test and Test2, you can use the following code:
class Test {
int a, b;
public:
Test() : a(0), b(0) {}
void Modify() { a++; b++; }
};

class Test2 {
int c, d;
public:
Test2() : c(1), d(1) {}
void Modify() { c++; d++; }
};

template<class T> void Modify(T& t)
// you used call-by-value and I think it is wrong
{
t.Modify();
}

void f()
{
Test t1;
Test2 t2;
Modify(t1);
Modify(t2);
}

Regards,
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top