Like Tree1Likes
  • 1 Post By manasij7479

Using new operator for template class

This is a discussion on Using new operator for template class within the C++ Programming forums, part of the General Programming Boards category; How would I use the new operator to create a new instantiation of a template class? For instance, If I ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Like this
    Code:
    dumb = new DumbClass<int>();
    wildcard_seven likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    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, 04:17 AM
  4. class template and operator=()
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2003, 11:42 PM

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