Thread: Using new operator for template class

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    86

    Using new operator for template class

    How would I use the new operator to create a new instantiation of a template class?

    For instance, If I had a template class like

    Code:
    template <class T> class DumbClass {
    
    public:
      // default constructor
      DumbClass( ) {}
    
    };
    Then I make a pointer like

    Code:
    DumbClass<int>* dumb;
    how do I dynamically allocate a DumbClass object to dumb, using the new operator--preferably without getting a compiler error?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Like this
    Code:
    dumb = new DumbClass<int>();

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    86
    Yap. I figured that out in a round about way just before I checked back here. Thanks for the response.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    auto dumb = new DumbClass<int>;
    Or
    auto * dumb = new DumbClass<int>;

    Btw, prefer to use smart pointers:
    auto dumb = std::make_shared<DumbClass<int>>();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overload with template class
    By Micko in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2011, 04:57 AM
  2. operator+ for template class
    By MarkZWEERS in forum C++ Programming
    Replies: 5
    Last Post: 05-07-2008, 03:55 PM
  3. Replies: 7
    Last Post: 11-10-2007, 05:17 AM
  4. class template and operator=()
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2003, 11:42 PM