473,320 Members | 1,863 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,320 software developers and data experts.

C++ Feature Help

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
6 1157
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
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

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

Similar topics

16
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that...
10
by: techy techno | last post by:
Hiii Hello.. I just wanted to know if anyone can tell me how can I give my website visitors the feature of "FRIENDLY PRINTING" through IE. I would definitely like to give a feature like...
3
by: dayzman | last post by:
Hi, I've read somewhere that feature-based analysis can be used to extract the semantic structure of HTML documents. By semantic structure, they mean the model of the rendered view a reader...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
1
by: Roshawn | last post by:
Hi, Has anyone seen the navigation feature used on www.projectseven.com? I think it's awesome!! All week long I've tried every trick in the book to imitate this feature using css but to no...
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
12
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None...
1
by: buts101 | last post by:
I've install XAMPP for php,mysql. I use Zend Studio for writing php,mysql codes. I know Zend Studio 5.5 has debug feature for php codes. How can i use the debug feature of zend studio with...
12
by: =?Utf-8?B?RGFyYSBQ?= | last post by:
Would like to know from the crowd that why do we require a Partial Class. By having such feature aren't we going out of the scope of Entity Concept. Help me to understand in what context this...
10
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.