-
November 5th, 2014, 10:32 AM
#1
A question regarding templated function
What is the real difference between a templated function defined in a class and a function defined in a templated class?
Code:
template <class T>
class A
{
void f();
};
class A
{
template <class T>
void f();
};
In terms of function f, I am not aware of any difference. But actually they do have difference in using them. For example, I can define first function f as virtual but I can't do the same thing for second function f. Thanks.
-
November 5th, 2014, 12:48 PM
#2
Re: A question regarding templated function
Eight threads on the first page, and in nine years on the forum, you haven't helped anybody ever. Not once. You should buy a book, take a class, learn how Google works or something.
Last edited by GCDEF; November 5th, 2014 at 12:50 PM.
-
November 6th, 2014, 07:39 AM
#3
Re: A question regarding templated function
the first needs a templated class instantiation to call a (regular) member function
the second needs a (regular) class instantiation to call a templatized member function.
That should have been obvious, no ?
-
November 6th, 2014, 08:45 PM
#4
Re: A question regarding templated function
Originally Posted by OReubens
the first needs a templated class instantiation to call a (regular) member function
the second needs a (regular) class instantiation to call a templatized member function.
That should have been obvious, no ?
Basically I understand that. But in terms of how to use the functions, is there any difference? Or let me put it this way. Is there any difference between a function defined in a templated class and a templated function defined in a regular class? For example, I can define first function as virtual but I can't defined second function as virtual, why? Thanks.
-
November 7th, 2014, 07:52 AM
#5
Re: A question regarding templated function
and how would you virtualize a function that doesn't actually exist until it is called (and generated) for the first time (with a concrete type) ?
again... if you'd have thought about it, that should have been obvious.
the 2 approaches are not the same, they serve different needs under different circumstances.
if you pull stuff out of context into a minimalistic example, then yes, it is still a valid example from a C++ point of view, but it may seem to not make a whole lot of difference at that point.
I can ask the same thing about a bus and a car. if I reduce both to 4 tires, an engine and a steering wheel... then what is the point of a bus and a car, why do both exist, they're seemingly idenitical ?
So the challenge to you.. try coming up with actual real-world cases where you would use one method and another where you'd use the other. Then see if both approaches are still as interchangable as the minimalistic sample makes it out to be.
-
November 7th, 2014, 08:50 AM
#6
Re: A question regarding templated function
Originally Posted by LarryChen
Basically I understand that. But in terms of how to use the functions, is there any difference? Or let me put it this way. Is there any difference between a function defined in a templated class and a templated function defined in a regular class? For example, I can define first function as virtual but I can't defined second function as virtual, why? Thanks.
The question isn't actually "what is the difference". That's irrelevant. Your question is actually: "Why can't template functions be virtual". If you literally type this in google ( http://lmgtfy.com/?q=Why+can%27t+tem...ons+be+virtual ), then the first 3 links will answer your question.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 7th, 2014, 02:44 PM
#7
Re: A question regarding templated function
Originally Posted by monarch_dodra
The question isn't actually "what is the difference". That's irrelevant. Your question is actually: "Why can't template functions be virtual". If you literally type this in google ( http://lmgtfy.com/?q=Why+can%27t+tem...ons+be+virtual ), then the first 3 links will answer your question.
I may be missing something, but wouldn't that require the use of Google?
-
November 8th, 2014, 06:56 AM
#8
Re: A question regarding templated function
Originally Posted by Arjay
I may be missing something, but wouldn't that require the use of Google?
you mean... as opposed to letting other people do the searching for you ? or the fact he's using lmgtfy (let me google that for you) ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|