CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2002
    Posts
    23

    Template functions within a Class

    Could someone tell me what the point of something like this would be...

    class Key : public KeyType
    {public:
    char value[20]

    Key(char* newKey)
    {strcpy(value, newKey);
    }

  2. #2
    Join Date
    Nov 2002
    Posts
    23
    ..sorry that bit of code was not finished when i posted....i'll continue it here..

    template<class KeyType>
    int operator == (KeyType & right){
    if(strcmp(value, right.value) ==0)
    return 1;
    else
    return 0;
    }

    template <class KeyType>
    int operator < (KeyType & right){
    if(strcmp(value, right.value) < 0)
    return 1;
    else
    return 0;
    }

    };

    I dont understand what the template <class KeyType> would accomplish here. And does the KeyType refer to the existing class by the name of 'KeyType' or is it treated like a generic name like Class T , ignoring that existing class.

    thanks

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    There is an aliase flaw in your design. The template class is KeyType and it is also derived from KeyType. I assume KeyType is the name of a real class, not what you pass into the template.

    template <class T>

    Kuphryn

  4. #4
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    That function can take multiple classes / structures which have a public value member variable. For example, it would work with a class and all of its deriviates.

    FYI, you can change you code to:

    template<class KeyType>
    int operator == (KeyType & right){
    return strcmp(value, right.value) == 0;
    }

    template <class KeyType>
    int operator < (KeyType & right){
    return strcmp(value, right.value) < 0;
    }

    There is no reason to ever code
    if( something == 7 )
    return true;
    return false;


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