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 6688523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:24:48+00:00 2026-05-26T05:24:48+00:00

I am designing a wrapper class (a bit similar to std::autoPtr but I have

  • 0

I am designing a wrapper class (a bit similar to std::autoPtr but I have different purpose) for scalar values:

template <typename T>
class ScalarPtr
{
private:
   T* m_data;
   ...
public:
   ScalarPtr(T *data): m_data(data)
   { ... }
   T& operator* ();
   T* operator -> ();
   ~ScalarPtr() 
   {
      if(m_data) 
         delete m_data; ...
   }
};

Now the problem is that when I also want to use this class for stack-allocated memory objects like this:

float temp=...
ScalarPtr<float> fltPtr(&temp);

The naive way is to pass boolean in constructor to specify whether to deallocate or not but is there any better way?

  • 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-26T05:24:49+00:00Added an answer on May 26, 2026 at 5:24 am

    I am not sure if there is a better approach other than the boolean flag.
    As you are aware(and hence ask the Q)this makes the interface rather non-intutive to the end user.

    The purpose of the wrapper/resource managing class is to implement an RAII, where the resource itself takes care of releasing its resources(in this case dynamic memory) implicitly. Given that the stack variables are automatically destroyed beyond their scopes,its seems rather odd to use a resource managing wrapper for them. I would rather not prefer to do so.

    But, Given that you want to maintain a uniform acess to your class through this wrapper class, the simplest yet not so elegant way seems to be the boolean flag.

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

Sidebar

Related Questions

I have a simple mysqli wrapper class I use for my database operations. I
I have a templated class template<class U, class V, class W> class S {
Designing forms has always been fun, but getting them to send email on the
When designing user table what would be the must have fields from the security/user
When designing a collection class, is there any reason not to implement locking privately
I'm in the middle of designing http://www.vanessacaroline.com and in need of a bit of
In designing a solution, sometimes it may be convenient to provide wrapper classes for
I have a type class Atomic , which defines functions for converting certain types
I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances
I have been designing a game for a University project, and I thought it

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.