Fun with templates.

This is a discussion on Fun with templates. within the C++ Programming forums, part of the General Programming Boards category; Just messing around with templates. It works, just wondering if anyone could see any use for what I'm doing. Code: ...

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068

    Fun with templates.

    Just messing around with templates. It works, just wondering if anyone could see any use for what I'm doing.
    Code:
    class Human { /* No Implementation, yet */ };
    
    class Male : public Human {
      public:
        void Speak() { cout << "I am a man." << endl; }
    };
    class Female : public Human {
      public:
        void Speak() { cout << "I am a woman." << endl; }
    };
    class Adult : public Human {
      public:
        void Speak() { cout << "I am an adult." << endl; }
    };
    class Child : public Human {
      public:
        void Speak() { cout << "I am a child." << endl; }
    };
    
    template <class Gender, class Age>
    class Individual : public Gender, public Age {
      public:
        void Speak() {
          Gender::Speak();
          Age::Speak();
        }
    };
    
    int main() {
       Individual<Male, Adult> man;
       Individual<Female, Adult> woman;
       Individual<Female, Child> girl;
    
       man.Speak();
       cout << endl;
       woman.Speak();
       cout << endl;
       girl.Speak();
    
       return 0;
    }
    Last edited by SlyMaelstrom; 03-22-2006 at 05:03 PM.
    Sent from my iPadŽ

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by SlyMaelstrom
    J
    Code:
    template <class Gender, class Age>
    class Individual : public Gender, public Age
    this is often referred to as the Curiously Recurring Template Pattern and has lots of uses.

    an easy one off the top of my head is a Singleton template
    Code:
    template <typename SINGLETON_TYPE>
    class Singleton
    {
        static SINGLETON_TYPE* pInstance;
    public:
        static inline SINGLETON_TYPE* Instance()
        {
             if (!pInstance)
             {
                  pInstance = new SINGLETON_TYPE();
             }
             return pInstance;
         }
    };
    
    class x : public Singleton<x>
    {
    private:
         friend class Singleton;
         x()
        {}
    };
    that's obviously not complete, but you get the idea.

    Templates are real fun when you get into specializations and template meta programming (ow, my brain hurts!)

    If you're interested, have a read of
    C++ Templates: The Complete Guide by Josuttis and Vandevoorde.

    Then buy Modern C++ Design by Alexandrescu to learn where the language is going. It will fry your brain!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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