![]() |
Syntax for template for member function of multiple classes?
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 |
Re: Syntax for template for member function of multiple classes?
Howard wrote:
> 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 |
Re: Syntax for template for member function of multiple classes?
Howard wrote: > 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. |
Re: Syntax for template for member function of multiple classes?
Howard wrote: > 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. |
Re: Syntax for template for member function of multiple classes?
Howard wrote:
> 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; } |
Re: Syntax for template for member function of multiple classes?
Howard wrote: > 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. |
Re: Syntax for template for member function of multiple classes?
"red floyd" <no.spam@here.dude> wrote in message news:Y4ynh.14047$sR.35@newssvr29.news.prodigy.net. .. > Howard wrote: >> 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 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 |
| All times are GMT. The time now is 07:56 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.