Thread: Template Question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    Template Question

    Hello fellow coders,

    I am trying to create a template that behaves differently
    if it is give a primative type vs a class. I should be easy
    enough but my brain seems to be stuck in neutral.

    Here is the general problem:

    Code:
    template< class T, class S>
    
    struct Node
    {
        Node* MyNode;
        T Value;
    }
    
    
    template< class T, class S>
    
    class Myclass
    {
        T GetValue();
        bool operator== (T rhs);
        Node node;
    }
    
    
    // In the case that T is a class, T will look somethink like this.
    
    template< class T, class S>
    
    class MyVector
    {
        int size;
        float length;
        S value;
    }
    Here are the requirements.

    If T is a a primative (int, long, float, double, char...), S will be a primative of the same type

    GetValue will return the value of classInst1.node.value
    The operator== will compare classInst1.node.value == classInst2.node.value


    If T is a class, S will be a primative.

    GetValue will return the value of classInst1.node.value
    The operator== will compare classInst1.node.<T>value.value == classInst2.node.<T>value.value


    so I could have a program like this:

    Code:
    main()
    {
        Myclass<int, int>         Foo1;
        Myclass<int, int>         Foo2;
        Myclass<MyVector, float>  Bar1;
        Myclass<MyVector, float>  Bar2;
    
        int      myInt  = Foo1.GetValue(); //returns Foo1.node.value
        MyVector myVect = Bar1.GetValue(); //returns Bar1.node.value
    
        if ( Foo1 == Foo2 ){}  //compares Foo1.node.value       == Foo2.node.value
        if ( Bar1 == Bar2 ){}  //compares Bar1.node.value.value == Bar2.node.value.value
    }
    Thank in advance for any help.

    byteherder
    Last edited by byteherder; 07-31-2011 at 04:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template question
    By darren78 in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2010, 08:47 AM
  2. Template question
    By borat in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2010, 02:52 AM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 03:52 PM
  4. Template Question
    By Eber Kain in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2004, 10:07 PM
  5. Another template question
    By grscot in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 06:16 PM