By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

C++ Feature Help

P: n/a
hi All,

1. Can I declare some of template functions into a class? a concrete
class, not the template class.

2. How do I do the function pointer in C++? Since that function
pointer is able to aceess any the functions inside any class.
Please advice.

Thanks.

Chee Siong

Aug 8 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ch*******@gmail.com wrote:
>
2. How do I do the function pointer in C++? Since that function
pointer is able to aceess any the functions inside any class.
Perhaps this will help:

http://www.parashift.com/c++-faq-lit...o-members.html

regards,
->HS

Aug 8 '07 #2

P: n/a
ch*******@gmail.com wrote:
1. Can I declare some of template functions into a class? a concrete
class, not the template class.
Yes, you should be able to.
2. How do I do the function pointer in C++?
You don't "do" a function pointer. You can declare it, like this:

void (*functionpointer)(int, double);

'functionpointer' is a pointer to a function that takes two arguments
and returns nothing.
Since that function
pointer is able to aceess any the functions inside any class.
Pointers to non-static member functions are not compatible with plain
pointers to functions. What book on C++ are you reading that does not
explain those concepts?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '07 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f9**********@news.datemas.de...
ch*******@gmail.com wrote:
>1. Can I declare some of template functions into a class? a concrete
class, not the template class.

Yes, you should be able to.
I didn't believe it, so I tried it, and you're right, it does work. The
output of the following program is
10 3.14159

#include <iostream>

class Foo
{
public:
template <class TT Bar( T t ) { return t; };
};

int main()
{
Foo MyFoo;

std::cout << MyFoo.Bar<int>( 10 ) << " " << MyFoo.Bar<double>(
3.1415926 );

return 0;
}

At first I couldn't understand how it could work, wouldn't there need to be
an instance of the class for each type? Then I realized the compiler only
has to make one copy of the method for each type. Interesting. Static
variables (of type T anyway) don't seem to be saved between calls however.
>2. How do I do the function pointer in C++?

You don't "do" a function pointer. You can declare it, like this:

void (*functionpointer)(int, double);

'functionpointer' is a pointer to a function that takes two arguments
and returns nothing.
>Since that function
pointer is able to aceess any the functions inside any class.

Pointers to non-static member functions are not compatible with plain
pointers to functions. What book on C++ are you reading that does not
explain those concepts?

Aug 9 '07 #4

P: n/a
Jim Langston wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f9**********@news.datemas.de...
>ch*******@gmail.com wrote:
>>1. Can I declare some of template functions into a class? a concrete
class, not the template class.

Yes, you should be able to.

I didn't believe it, so I tried it, and you're right, it does work.
What book are you reading that doesn't explain member templates?
The output of the following program is
10 3.14159

#include <iostream>

class Foo
{
public:
template <class TT Bar( T t ) { return t; };
};

int main()
{
Foo MyFoo;

std::cout << MyFoo.Bar<int>( 10 ) << " " << MyFoo.Bar<double>(
3.1415926 );
You don't even need to specify the template argument. '10' is int
and '3.14...' is a double. The compiler will deduce the template
argument from the function call.
>
return 0;
}

At first I couldn't understand how it could work, wouldn't there need
to be an instance of the class for each type? Then I realized the
compiler only has to make one copy of the method for each type.
Interesting. Static variables (of type T anyway) don't seem to be
saved between calls however.
What static variables?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '07 #5

P: n/a
On Aug 9, 2:38 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:f9**********@news.datemas.de...
cheesi...@gmail.com wrote:
1. Can I declare some of template functions into a class? a concrete
class, not the template class.
Yes, you should be able to.
I didn't believe it,
You probably learned C++ a long time ago, then. You couldn't do
it in ARM C++; it was added by the standardization committee.
so I tried it, and you're right, it does work. The
output of the following program is
10 3.14159
#include <iostream>
class Foo
{
public:
template <class TT Bar( T t ) { return t; };
};
int main()
{
Foo MyFoo;

std::cout << MyFoo.Bar<int>( 10 ) << " " << MyFoo.Bar<double>(
3.1415926 );
return 0;
}
At first I couldn't understand how it could work, wouldn't
there need to be an instance of the class for each type? Then
I realized the compiler only has to make one copy of the
method for each type. Interesting. Static variables (of type
T anyway) don't seem to be saved between calls however.
They should be. Could you give an example? (Remember that a
function template is NOT a function. Each instantiation of a
function template is a function. So each instantiation will
have its own local static variables.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 9 '07 #6

P: n/a
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Aug 9, 2:38 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:f9**********@news.datemas.de...
cheesi...@gmail.com wrote:
1. Can I declare some of template functions into a class? a concrete
class, not the template class.
Yes, you should be able to.
I didn't believe it,
You probably learned C++ a long time ago, then. You couldn't do
it in ARM C++; it was added by the standardization committee.
so I tried it, and you're right, it does work. The
output of the following program is
10 3.14159
#include <iostream>
class Foo
{
public:
template <class TT Bar( T t ) { return t; };
};
int main()
{
Foo MyFoo;

std::cout << MyFoo.Bar<int>( 10 ) << " " << MyFoo.Bar<double>(
3.1415926 );
return 0;
}
At first I couldn't understand how it could work, wouldn't
there need to be an instance of the class for each type? Then
I realized the compiler only has to make one copy of the
method for each type. Interesting. Static variables (of type
T anyway) don't seem to be saved between calls however.
They should be. Could you give an example? (Remember that a
function template is NOT a function. Each instantiation of a
function template is a function. So each instantiation will
have its own local static variables.)

==========

I wish I had saved the code I was experimenting with. Because I thought the
same thing, and right now it is saving the instances of the static T
variables between calls, but it wasn't before. Unfortunately, I can't
reproduce it. I did what I thought was the exact same thing but now it's
retaining the value. Either some compiler bug, or most likely, PBKAC.
Aug 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.