Function template in Class template?

This is a discussion on Function template in Class template? within the C++ Programming forums, part of the General Programming Boards category; What's the syntax for a "function template" in a "class template"? Code: template <class ClsType> class Class { template <typename ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Question Function template in a Class template?

    What's the syntax for a "function template" in a "class template"?

    Code:
    template <class ClsType>
    class Class {
    	template <typename FuncType>
    	void Function(FuncType Object);
    };
    
    template <class ClsType>
    template <typename FuncType>
    void Class<ClsType>::Function(FuncType Object) {
    	//...
    }
    How should this ex. have been written to compile correctly?
    (Please note that I don't wish ClsType and FuncType to be equal.)

    Thanks in advance
    Last edited by Aidman; 10-28-2003 at 07:04 AM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    template <class ClsType>
    void Class<ClsType>::
    template <typename FuncType>
    Function(FuncType Object)
    //...

    Is how I tried to do it, but finally gave up and just wrote it inline usint MSVC6.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,643
    Member Function Templates

    Notice that they are supported (by the MS compiler) only if fully defined within the enclosing class.

    Example:
    Code:
    template <typename T>
    class A
    {
    public:
        T t;
    
        template <typename C>
            void assign(const C &c) {t = (T)c;}
    };
    
    
    int main()
    {
        A<int> a;
    
        a.t = 1;
    
        long val = 4;
    
        a.assign(val);
    
        cout << a.t << endl;
    
        return 1;
    }
    gg

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    very helpful. Thankyou.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Data init inside of template class function
    By VirtualAce in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2002, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21