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

  • Home
  • SEARCH
  • 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 3219420
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:38:37+00:00 2026-05-17T15:38:37+00:00

I’m working with a class for which the new operator has been made private,

  • 0

I’m working with a class for which the new operator has been made private, so that the only way to get an instance is to write

Foo foo = Foo()

Writing

Foo* foo = new Foo()

does not work.

But because I really want a pointer to it, I simulate that with the following :

Foo* foo = (Foo*)malloc(sizeof(Foo));
*foo = Foo();

so that can test whether the pointer is null to know whether is has already been initialized.

It looks like it works, from empirical tests, but is it possible that not enough space had been allocated by malloc ? Or that something else gets funny ?

— edit —

A didn’t mention the context because I was not actually sure about why they the new operator was disabled. This class is part of a constraint programming library (gecode), and I thought it may be disabled in order to enforced the documented way of specifying a model.

I didn’t know about the Concrete Data Type idiom, which looks like a more plausible reason.

That allocation scheme may be fine when specifying a standard model — in which everything is specified as CDTs in the Space-derived class — but in my case, these instance are each created by specific classes and then passed by reference to the constructor of the class that reprensents the model.

About the reason i’m not using the

Foo f;
Foo *pf = &f;

it would be like doing case 1 below, which throws a “returning reference to local variable” warning

int& f() { int a=5; return a; } // case 1
int& f() { int a=5; int* ap=&a; return *ap; }
int& f() { int* ap=(int*)malloc(sizeof(int)); *ap=5; return *ap; }

this warning disappears when adding a pointer in case 2, but I guess it is because the compiler loses tracks.

So the only option left is case 3 (not mentioning that additionaly, ap is a member of a class that will be initialized only once when f is called, will be null otherwise, and is the only function returning a reference to it. That way, I am sure that ap in this case when lose its meaning because of the compilier optimizing it away (may that happen ?)

But I guess this reaches far too much beyond the scope of the original question now…

  • 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-17T15:38:37+00:00Added an answer on May 17, 2026 at 3:38 pm

    Be careful about breaking the Concrete Data Type idiom.

    You are trying to circumvent the fact that the new operator has been made private, i.e. the Concrete Data Type idiom/pattern. The new operator was probably made private for specific reasons, e.g. another part of the design may depend on this restriction. Trying to get around this to dynamically allocate an instance of the class is trying to circumvent the design and may cause other problems or other unexpected behavior. I wouldn’t suggest trying to circumvent this without studying the code thoroughly to ensure you understand the impact to other parts of the class/code.

    Concrete Data Type

    • http://users.rcn.com/jcoplien/Patterns/C++Idioms/EuroPLoP98.html#ConcreteDataType

    Solutions

    …

    Objects that represent abstractions that live "inside" the program, closely tied to the computational model, the implementation, or the programming language, should be declared as local (automatic or static) instances or as member instances. Collection classes (string, list, set) are examples of this kind of abstraction (though they may use heap data, they themselves are not heap objects). They are concrete data types–they aren’t "abstract," but are as concrete as int and double.

    class ScopedLock
    {
      private:
        static void * operator new (unsigned int size); // Disallow dynamic allocation
        static void * operator new (unsigned int size, void * mem);  // Disallow placement new as well.
    };
    int main (void)
    {
       ScopedLock s; // Allowed
       ScopedLock * sl = new ScopedLock (); // Standard new and nothrow new are not allowed.
       void * buf = ::operator new (sizeof (ScopedLock));
       ScopedLock * s2 = new(buf) ScopedLock;  // Placement new is also not allowed
    }
    

    ScopedLock object can’t be allocated dynamically with standard uses of new operator, nothrow new, and the placement new.

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

Sidebar

Related Questions

No related questions found

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.