C++ Feature Help

C

cheesiong

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
 
V

Victor Bazarov

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
 
J

Jim Langston

Victor Bazarov said:
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 T> T 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.
 
V

Victor Bazarov

Jim said:
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 T> T 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
 
J

James Kanze

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 T> T 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.)
 
J

Jim Langston

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 T> T 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top