// MyClass.cpp
#include <AllTheStuff>
template <class Any>
void MyFunction(Any &a, Any &b)
{
// do something
}
but what about the MyClass:: operator before MyFunction and what is the
stuff I write in my header.
// MyClass.h
{
public:
// how I write the funcion prototype here??
private:
}
--
Daniel Samson
Daniel.Sa...@mrq.gouv.qc.ca
Daniel Samson a écrit:
It seems to me that you consider a "function" to be a globally available
routine, while a "method" is a routine that is a member of a class. I
don't think it's a good idea to rely on programmers in general
understanding that distinction. I certainly wouldn't except for the
context of the rest of your message.
I believe that the original poster was asking how to declare class
member functions that use templates, as in:
// MyClass.h
{
public:
// how I write the funcion prototype here??
// My attempt, not tested or in any way otherwise
guaranteed -- Rob
template <class Any>
void foo(Any& a, Any& b)
{
// Implementation of foo() would have to be here
rather than
// in MyClass.pp
}
private:
}
I seem to remember that such templatized member functions are not often
suppored in current compilers, but I could be wrong.
So, what is the answer to the original question?
Rob
Daniel Samson
Daniel.Sa...@mrq.gouv.qc.ca
> I want to include a template function in an own class. Following my book, I
> wrote something like this:
>
> // MyClass.cpp
> #include <AllTheStuff>
> template <class Any>
> void MyFunction(Any &a, Any &b)
> {
> // do something
> }
>
> but what about the MyClass:: operator before MyFunction and what is the
> stuff I write in my header.
>
> // MyClass.h
> {
> public:
> // how I write the funcion prototype here??
>
> private:
> }
>
> --
>
> Daniel Samson
> Daniel.Sa...@mrq.gouv.qc.ca
Hi,
Please see below.
Alex
###################################################
############# 1. MyClass.h ########################
###################################################
#include <string>
//############################################
template <class T1, class T2>
void foo1(T1 &a, T2 &b)
{
cout << __PRETTY_FUNCTION__ << " : a = " << a << endl;
cout << __PRETTY_FUNCTION__ << " : b = " << b << endl;
cout << endl;
}
//############################################
template <class T1, class T2>
void foo2()
{
cout << __PRETTY_FUNCTION__ << " : No input parameters" << endl;
cout << endl;
}
//############################################
class MyClass
{
public:
MyClass () {}
~MyClass () {}
template <class T1, class T2>
void Foo1(T1 &a, T2 &b);
template <class T1, class T2>
void Foo2();
private:
};
template <typename T1, typename T2>
void MyClass::Foo1(T1 &a, T2 &b)
{
cout << __PRETTY_FUNCTION__ << " : a = " << a << endl;
cout << __PRETTY_FUNCTION__ << " : b = " << b << endl;
cout << endl;
}
template <typename T1, typename T2>
void MyClass::Foo2()
{
cout << __PRETTY_FUNCTION__ << " : No input parameters" << endl;
cout << endl;
}
###################################################
############# 2. MyClass.cpp ######################
###################################################
int main ()
{
MyClass my;
int i1 = 123;
int i2 = 79;
char* c1 = "abcd";
char* c2 = "xyz";
cout << "###### foo1 ######" << endl;
foo1 (i1, i2);
foo1 (i1, c2);
foo1 (c1, c2);
foo1 (c1, i2);
cout << "###### foo2 ######" << endl;
foo2<int, int> ();
foo2<int, char*> ();
foo2<char*, int> ();
foo2<char*, char*> ();
cout << "###### MyClass::Foo1 ######" << endl;
my.Foo1 (i1, i2);
my.Foo1 (i1, c2);
my.Foo1 (c1, c2);
my.Foo1 (c1, i2);
cout << "###### MyClass::Foo2 ######" << endl;
my.template Foo2<int, int> ();
my.template Foo2<int, char*> ();
my.template Foo2<char*, int> ();
my.template Foo2<char*, char*> ();
return 0;
}
###################################################
############# 3. Compiler #########################
###################################################
g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
###################################################
############# 4. Hardware #########################
###################################################
uname -a : SunOS tibamsun8 5.6 Generic_105181-09 sun4m sparc
SUNW,SPARCstation-5
###################################################
############# 5. Results of the running ##########
###################################################
###### foo1 ######
void foo1<int, int>(int &, int &) : a = 123
void foo1<int, int>(int &, int &) : b = 79
void foo1<int, char *>(int &, char *&) : a = 123
void foo1<int, char *>(int &, char *&) : b = xyz
void foo1<char *, char *>(char *&, char *&) : a = abcd
void foo1<char *, char *>(char *&, char *&) : b = xyz
void foo1<char *, int>(char *&, int &) : a = abcd
void foo1<char *, int>(char *&, int &) : b = 79
###### foo2 ######
void foo2<int, int>() : No input parameters
void foo2<int, char *>() : No input parameters
void foo2<char *, int>() : No input parameters
void foo2<char *, char *>() : No input parameters
###### MyClass::Foo1 ######
void MyClass::Foo1<int, int>(int &, int &) : a = 123
void MyClass::Foo1<int, int>(int &, int &) : b = 79
void MyClass::Foo1<int, char *>(int &, char *&) : a = 123
void MyClass::Foo1<int, char *>(int &, char *&) : b = xyz
void MyClass::Foo1<char *, char *>(char *&, char *&) : a = abcd
void MyClass::Foo1<char *, char *>(char *&, char *&) : b = xyz
void MyClass::Foo1<char *, int>(char *&, int &) : a = abcd
void MyClass::Foo1<char *, int>(char *&, int &) : b = 79
###### MyClass::Foo2 ######
void MyClass::Foo2<int, int>() : No input parameters
void MyClass::Foo2<int, char *>() : No input parameters
void MyClass::Foo2<char *, int>() : No input parameters
void MyClass::Foo2<char *, char *>() : No input parameters
###################################################
############# END of INFORMATION #################
###################################################