How to disable regular constructor and only allow new?

Discussion in 'C++' started by Peng Yu, Sep 9, 2011.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I want to make sure objects of a class are always created in the heap
    rather than the stack. Could anybody let me how to do so?

    Regards,
    Peng
    Peng Yu, Sep 9, 2011
    #1
    1. Advertising

  2. Peng Yu <> wrote:
    > I want to make sure objects of a class are always created in the heap
    > rather than the stack.


    I'm curious to know why.
    Juha Nieminen, Sep 9, 2011
    #2
    1. Advertising

  3. Peng Yu

    Goran Guest

    On Sep 9, 5:47 am, Peng Yu <> wrote:
    > Hi,
    >
    > I want to make sure objects of a class are always created in the heap
    > rather than the stack. Could anybody let me how to do so?
    >
    > Regards,
    > Peng


    class on_heap;
    typedef auto_ptr<on_heap> ap_on_heap;
    class on_heap
    {
    private:
    on_heap(params) {...}
    public:
    static ap_on_heap create(params)
    {
    return ap_on_heap(new on_heap(params));
    }
    };

    If you want to derive from on_heap, make constructors protected.

    Goran.
    Goran, Sep 9, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    2
    Views:
    948
    clintonG
    Sep 19, 2006
  2. WittyGuy
    Replies:
    5
    Views:
    449
    James Kanze
    Apr 26, 2007
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,229
  4. Warren Tang
    Replies:
    9
    Views:
    670
    James Kanze
    Apr 15, 2008
  5. Ryan Taylor
    Replies:
    1
    Views:
    685
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page