Syntax for template for member function of multiple classes?

H

Howard

Hi,

I have a function in three unrelated but similar classes. The code in the
member functions is identical for all three classes. What I want is to make
a template which defines the function, implemented as a non-static member of
each class. Aside from this function, there is no reason the three classes
should inherit from a common base class, which is why I thought a template
would be good. But I can't figure out the syntax for a template which
specifies itself as a member function of the specified class. Also, how
would each class declare the member function?

I tried variations of this, but no luck:

template <class T>
void <T>::foo() // syntax errors here
{
// ...do stuff here...
}

class A
{
void foo<A>();
};

class B
{
void foo<B>();
};

class C
{
void foo<C>();
};

Thanks,
-H
 
R

red floyd

Howard said:
Hi,

I have a function in three unrelated but similar classes. The code in the
member functions is identical for all three classes. What I want is to make
a template which defines the function, implemented as a non-static member of
each class. Aside from this function, there is no reason the three classes
should inherit from a common base class, which is why I thought a template
would be good. But I can't figure out the syntax for a template which
specifies itself as a member function of the specified class. Also, how
would each class declare the member function?

I tried variations of this, but no luck:

template <class T>
void <T>::foo() // syntax errors here
{
// ...do stuff here...
}

class A
{
void foo<A>();
};

class B
{
void foo<B>();
};

class C
{
void foo<C>();
};

I think your best bet is something like this:

template< class T >
class FooClass : public T
{
public: // I asume you wanted this
void foo();
};

Otherwise, I think you are pretty much oiut of luck
 
P

psp

Howard said:
Hi,

I have a function in three unrelated but similar classes. The code in the
member functions is identical for all three classes. What I want is to make
a template which defines the function, implemented as a non-static member of
each class. Aside from this function, there is no reason the three classes
should inherit from a common base class, which is why I thought a template
would be good. But I can't figure out the syntax for a template which
specifies itself as a member function of the specified class. Also, how
would each class declare the member function?

I tried variations of this, but no luck:

template <class T>
void <T>::foo() // syntax errors here
{
// ...do stuff here...
}

class A
{
void foo<A>();
};

class B
{
void foo<B>();
};

class C
{
void foo<C>();
};

Thanks,
-H
I think declaring this function as a friend of all 3 classes is
appropriate. Then you can define this function template wherever you
want.
 
P

psp

Howard said:
Hi,

I have a function in three unrelated but similar classes. The code in the
member functions is identical for all three classes. What I want is to make
a template which defines the function, implemented as a non-static member of
each class. Aside from this function, there is no reason the three classes
should inherit from a common base class, which is why I thought a template
would be good. But I can't figure out the syntax for a template which
specifies itself as a member function of the specified class. Also, how
would each class declare the member function?

I tried variations of this, but no luck:

template <class T>
void <T>::foo() // syntax errors here
{
// ...do stuff here...
}

class A
{
void foo<A>();
};

class B
{
void foo<B>();
};

class C
{
void foo<C>();
};

Thanks,
-H
I think declaring this function as a friend of all 3 classes is
appropriate. Then you can define this function template wherever you
want.
 
B

bjeremy

Howard said:
Hi,

I have a function in three unrelated but similar classes. The code in the
member functions is identical for all three classes. What I want is to make
a template which defines the function, implemented as a non-static member of
each class. Aside from this function, there is no reason the three classes
should inherit from a common base class, which is why I thought a template
would be good. But I can't figure out the syntax for a template which
specifies itself as a member function of the specified class. Also, how
would each class declare the member function?


You can use forwarding to do this:

template <class T>
void foo()
{
T *t = new T;
cout << "I am a: " << typeid(t).name() << endl;

delete t;
}

class A
{
public:
void doTheFoo() {foo<A>();}
};

class B
{
public:
void doTheFoo() {foo<B>();}
};

class C
{
public:
void doTheFoo(){foo<C>();}
};

int main(int argc,char **argv)
{

A a;
a.doTheFoo();

B b;
b.doTheFoo();

C c;
c.doTheFoo();

return 0;
}
 
G

Grizlyk

Howard said:
template <class T>
void <T>::foo() // syntax errors here
{
// ...do stuff here...
}

You are trying to declare a function foo() of class T outside of class
T declaration (T{};). This is wrong.
I have a function in three unrelated but similar classes. The code in the
member functions is identical for all three classes. What I want is to make
a template which defines the function, implemented as a non-static member of
each class.
In most cases, your desire is not good, because you are not using any
desing questions. Your conclusion "code in the member functions is
identical" is not design question, you must not have a look to the
classes in the manner while creating them.

If design does not matter to you here, just make the function outside
of classes, in any namespace.
 
H

Howard

I found a solution: I made a small class which holds just the data which is
operated on by those three functions, and made a single member function of
that new class to replace the three functions. Then I replaced that common
data with an instance of the new class (inside each of my three other
classes), and replaced calls to the old member functions with calls into
that new class's member function.

It occurs to me that what I was trying to do was more like a macro than a
template, and probably not the best design. It's working fine now.

Thanks all,
-H
 

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
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top