Thread: default template?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    default template?

    Here is the code:

    Code:
    class out {
    public:
    	int test;
    };
    class out2 : public out {
    public:
    	int test2;
    };
    
    template <class a_type>
    class cone {
    public:
    	a_type cobject;
    };
    
    class ctwo : public cone<out2> {
    public:
    	int test;
    };
    Can I have default template for templated class 'cone'?
    So compiler would use default_class if I dont specify any..
    For instance:

    Code:
    class default_class {
    }
    
    template <class a_type = default_class>
    class cone {
    public:
    	a_type cobject;
    };
    
    class ctwo : public cone {
    public:
    	int test;
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you try it? What errors did you get?

    I think this is what you want:
    Code:
    class ctwo : public cone<> {

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    #include <iostream>
    #include <string>
    
    class foo {
       public:
          std::string toString() {
             return "Hello World";
          }
    };
    
    class bar {
       public:
          std::string toString() {
             return "Goodbye!";
          }
    };
                
    
    template <typename A = foo> class Printer {
       public:
          A myA;
          void print() {
             std::cout << myA.toString() << '\n';
          }
    };
    
    int main (void) {
       Printer<> hello; // Uses foo by default
       hello.print(); // Prints "Hello World!" using foo
    
       Printer<foo> hello2; // Explicitly uses foo
       hello2.print(); // Prints "Hello World!" using foo
    
       Printer<bar> bye; // Explicitly uses bar
       bye.print(); // Prints "Goodbye!" using bar
    
       return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thank you guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM