469,920 Members | 2,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

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
Jan 5 '07 #1
6 1662
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


Jan 5 '07 #2
psp

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.

Jan 5 '07 #3
psp

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.

Jan 5 '07 #4
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;
}

Jan 5 '07 #5

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.

Jan 12 '07 #6

"red floyd" <no*****@here.dudewrote in message
news:Y4***************@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
Jan 12 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by jack | last post: by
8 posts views Thread by Rich Grise | last post: by
12 posts views Thread by mlimber | last post: by
2 posts views Thread by coolpint | last post: by
8 posts views Thread by Imre | last post: by
20 posts views Thread by W Karas | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.