template parameter on a function that dont use the template

This is a discussion on template parameter on a function that dont use the template within the C++ Programming forums, part of the General Programming Boards category; Hi, I just discovered this today, I wonder if this is standard enough? Code: class Boohwa{ public: void mooya(){ cout ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    template parameter on a function that dont use the template

    Hi,

    I just discovered this today, I wonder if this is standard enough?

    Code:
    class Boohwa{
       public:
          void mooya(){
             cout << "boohwa!" << endl;
          }
    };
    
    class What{
       public:
          
          template<class M>
          void mooya(){
             M moo;
             moo.mooya();
          }
          
          
    };
    and later, you can do:

    Code:
       What what;
       what.mooya<Boohwa>();
    Would love to hear where I can read more about this stuff, I was just experimenting around. This is potentially SUPER useful.. I'd imagine?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    That code is standard (as in it isn't against any C++ rules).

    This is potentially SUPER useful.. I'd imagine?
    Well its only useful if it solves some problem better than any other method

    If you want to expand on your idea a little more, you could always do something like:
    Code:
    class Boohwa{
       public:
          virtual void mooya(){
             cout << "boohwa!" << endl;
          }
    };
    
    template <class M>
    class What : public M
    {
    };
    
    // ...
    
    What<Boohwa> what;
    what.mooya();
    Templates can be pretty interesting since they can be very powerful. It can be easy to get yourself into trouble with very convoluted code if you're not careful though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 11:36 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  4. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 05:37 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM

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