Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

template function problem

0 views
Skip to first unread message

Daniel Samson

unread,
Jan 7, 1999, 3:00:00 AM1/7/99
to
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

Bernard Lecomte

unread,
Jan 8, 1999, 3:00:00 AM1/8/99
to Daniel Samson
If MyFunction is actually a function (as opposed to a method), you don't have
to declare it in your class, unless MyFunction calls protected or private
members of MyClass that force you to declare it in MyClass as a friend.
You just have to declare MyFunction outside the any class (just as any global
functions) in your header like that :
template <class Any> void MyFunction(Any &a, Any &b);

Daniel Samson a écrit:

Richardson, Robert D

unread,
Jan 8, 1999, 3:00:00 AM1/8/99
to
Bernard

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

unread,
Jan 8, 1999, 3:00:00 AM1/8/99
to

I effectively wanted to make a member function template and somebody told me
this fonctionnality is not include in C++ Builder yet. It's great that
newsgroups exist 'cause I may already shake my head wondering what is not
working.

Daniel Samson
Daniel.Sa...@mrq.gouv.qc.ca

Alex Vinokur

unread,
Jan 10, 1999, 3:00:00 AM1/10/99
to Daniel Samson
Daniel Samson wrote:

> 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 #################
###################################################


0 new messages