CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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 ?

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding templated function

    Quote Originally Posted by OReubens View Post
    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.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A question regarding templated function

    Quote Originally Posted by LarryChen View Post
    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.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding templated function

    Quote Originally Posted by monarch_dodra View Post
    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?

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A question regarding templated function

    Quote Originally Posted by Arjay View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured