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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:12:08+00:00 2026-05-22T03:12:08+00:00

I read in this blog, that making the new operator private is a good

  • 0

I read in this blog, that making the new operator private is a good approach to enforce instantiation on the stack.

I am implementing a class that employs the RAII idiom. This class should obviously only be instantiated on the stack, so I am looking for a way to enforce that.

  • My question is, does this have any side effects that are not straight-forward to see?
  • Is it a good approach to enforce instantiation on the stack?
  • Are there any portability issues?

Thanks for your help!

EDIT

My RAII class just instantiates various parts of the framework I’m working on, so it makes no sense to do anything else with that class than creating an instance on the stack.

The goal is just to provide a simple possiblity to configure the framework and put it in a ready-to-use state, without having to instantiate 10 objects in the client code.

  • 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-22T03:12:09+00:00Added an answer on May 22, 2026 at 3:12 am

    I am implementing a class that employs the RAII idiom. This class should obviously only be instantiated on the stack, so I am looking for a way to enforce that.

    That is not RAII. RAII is initializing when constructing and de-initializing in destructors. RAII is about resources you control (e.g. data members) and not the lifetime of the “parent” object. Users of your type then again apply RAII to control the lifetime of what they allocate. For example, you can dynamically allocate a std::vector, even though vector uses RAII to make sure every item it “owns” is cleaned up.


    My question is, does this have any side effects that are not straight-forward to see?

    Yes, you prevent otherwise valid (at least as far as RAII is concerned) uses of your type.

    void example() {
      shared_ptr<T> p (new T());
      // No RAII violation, still uses operator new.
    
      vector<T> v (some_initial_size);
      // No RAII violation, still uses operator new (the placement form is used
      // inside the default allocator).
    }
    

    Is it a good approach to enforce instantiation on the stack?

    What are you trying to prevent? What is the use case? Anyone who uses new with your type either 1) knows what they’re doing and needs it, or 2) will screw up resource management horribly no matter what you do. You hinder those in #1 without helping those in #2 by attempting to enforce this.

    This is another way to state what Steve said:

    This class should obviously only be instantiated on the stack, so I am looking for a way to enforce that.

    If that is obvious, why does it need to be enforced?

    If users will blindly use “new T()”, what makes you think they won’t blindly use “::new T()”?


    The goal is just to provide a simple possiblity to configure the framework and put it in a ready-to-use state, without having to instantiate 10 objects in the client code.

    #include <framework>
    
    int main() {
      framework::Init init;
      // Do stuff.
    }
    

    Use this, or something very close to it, prominently in your docs’ examples.

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

Sidebar

Related Questions

I just read this blog post and it stated that: Google is now working
I recently read this blog entry by Hadi Hariri: That dreaded M in ASP.NET
I read this blog: After conducting a bit of research I discovered that this
I read this comment in the OpenID post on the stackoverflow blog. Kibbee says
I read at a few places (in the doc and in this blog post
I read at Scottgu blog about using OutputCache for a function but this didn't
I read this story on slashdot today where they announce a new parallel programming
I read on MySQL Performance Blog that when tables are large, it is better
I am making a personal blog that people can download and edit, but the
I just read this blog post on Razor Templating in ASP.NET MVC 3. Put

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.