Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 710017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:34:05+00:00 2026-05-14T04:34:05+00:00

I have an auto pointer class and in the constructor I am passing in

  • 0

I have an auto pointer class and in the constructor I am passing in a pointer. I want to be able to separate new from new[] in the constructor so that I can properly call delete or delete[] in the destructor. Can this be done through template specialization? I don’t want to have to pass in a boolean in the constructor.

    template <typename T>
    class MyAutoPtr
    {
    public:
      MyAutoPtr(T* aPtr);
    };

// in use:
MyAutoPtr<int> ptr(new int);
MyAutoPtr<int> ptr2(new int[10]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:34:05+00:00Added an answer on May 14, 2026 at 4:34 am

    std::unique_ptr in C++0x will have a specialization for dynamic arrays, somewhat like shown below. However, it will be the user’s task to instantiate an appropriate instance. At language level there is no way to distinguish one pointer from another.

    template <class T>
    class pointer
    {
        T* p;
    public:
        pointer(T* ptr = 0): p(ptr) {}
        ~pointer() { delete p; }
        //... rest of pointer interface
    };
    
    template <class T>
    class pointer<T[]>
    {
        T* p;
    public:
        pointer(T* ptr = 0): p(ptr) {}
        ~pointer() { delete [] p; }
        //... rest of pointer and array interface
    };
    
    int main()
    {
        pointer<int> single(new int);
        pointer<int[]> array(new int[10]);
    }
    

    Furthermore, it might not be that good to load one class with so various tasks. For example, boost has shared_ptr and shared_array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a auto_ptr I can pass it for a reference?Like: auto_ptr<MyClass>Class(new MyClass);
I have a template class that I've subclassed with a pointer to it (Decorator
I have an auto pointer implementation: template <typename T, bool Arr = false> class
I have a class that I've been provided that I really don't want to
I have this function which adds any class that derives from Object to a
I have Auto completed text view,i want to filter the items present in the
I have used auto complete textbox previously. That auto complete works only when i
I have an auto overflowed DIV on my page, what i want to do
I have a C++ class that contains a non-copyable handle. The class, however, must
I have next class and try to declare member function which will return pointer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.