473,388 Members | 986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 1837
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
3
by: jack | last post by:
Hi there, I have a function F(x, y, z) and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0) z0 is fixed. Suppose somebody wrote a routine called "Compute" which simply computes f(a)...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
8
by: Imre | last post by:
Hi I'm looking for a way to make sure that whenever a new instance of a class template A is created, then an instance of class template B is also created, with the same template parameters. Of...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.