Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Syntax for template for member function of multiple classes?

Reply
Thread Tools

Syntax for template for member function of multiple classes?

 
 
Howard
Guest
Posts: n/a
 
      01-05-2007
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


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-05-2007
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




 
Reply With Quote
 
 
 
 
psp
Guest
Posts: n/a
 
      01-05-2007

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.

 
Reply With Quote
 
psp
Guest
Posts: n/a
 
      01-05-2007

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.

 
Reply With Quote
 
bjeremy
Guest
Posts: n/a
 
      01-05-2007
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;
}

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007

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.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-12-2007

"red floyd" <(E-Mail Removed)> wrote in message
news:Y4ynh.14047$(E-Mail Removed). ..
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
specialized member function takes precedence over generic template member function bluekite2000@gmail.com C++ 1 07-20-2005 08:58 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Out-of-class-template member template specialization syntax BigMan C++ 1 03-26-2005 02:23 PM



Advertisments