Instantiation of an Object only thru "new Operator"

Discussion in 'C++' started by Mysooru, Jul 28, 2003.

  1. Mysooru

    Mysooru Guest

    How to restrict an object to be instantiated only using new operator?

    I mean, one should not be allowed to create an object like AnObject obj; it
    should be AnObject obj = new AnObject;
     
    Mysooru, Jul 28, 2003
    #1
    1. Advertising

  2. "Mysooru" <> wrote in message
    news:bg2f3h$djk$...
    > How to restrict an object to be instantiated only using new operator?
    >
    > I mean, one should not be allowed to create an object like AnObject obj;

    it
    > should be AnObject obj = new AnObject;
    >


    Make the constructor private and write a static member function that uses
    new

    class AnObject
    {
    public:
    static AnObject* createAnObject(); { return new AnObject(); }
    private:
    AnObject(); // private ctor
    };

    AnObject* obj = createAnObject();

    john
     
    John Harrison, Jul 28, 2003
    #2
    1. Advertising

  3. >> >How to restrict an object to be instantiated only using new operator?
    >> >
    >> >I mean, one should not be allowed to create an object like AnObject obj;

    >it
    >> >should be AnObject obj = new AnObject;

    >>
    >> And why would you want that?

    >
    >Perhaps because the object wants to do a "delete this;"?


    Isn't it allowed to do it even if it is an automatic object (assuming
    a placement new will be made) ?

    Jonathan
     
    Jonathan Mcdougall, Jul 28, 2003
    #3
  4. "Jonathan Mcdougall" <> wrote in message
    news:...
    > >> >How to restrict an object to be instantiated only using new operator?
    > >> >
    > >> >I mean, one should not be allowed to create an object like AnObject

    obj;
    > >it
    > >> >should be AnObject obj = new AnObject;
    > >>
    > >> And why would you want that?

    > >
    > >Perhaps because the object wants to do a "delete this;"?

    >
    > Isn't it allowed to do it even if it is an automatic object (assuming
    > a placement new will be made) ?
    >
    > Jonathan


    You can't delete this on an automatic object. What does placement new have
    to do with anything?

    john
     
    John Harrison, Jul 28, 2003
    #4
  5. On Mon, 28 Jul 2003 08:28:37 +0100, "John Harrison"
    <> wrote:

    >
    >"Jonathan Mcdougall" <> wrote in message
    >news:...
    >> >> >How to restrict an object to be instantiated only using new operator?
    >> >> >
    >> >> >I mean, one should not be allowed to create an object like AnObject

    >obj;
    >> >it
    >> >> >should be AnObject obj = new AnObject;
    >> >>
    >> >> And why would you want that?
    >> >
    >> >Perhaps because the object wants to do a "delete this;"?

    >>
    >> Isn't it allowed to do it even if it is an automatic object (assuming
    >> a placement new will be made) ?
    >>
    >> Jonathan

    >
    >You can't delete this on an automatic object. What does placement new have
    >to do with anything?


    Nothing. Who said that?


    A sad Jonathan
     
    Jonathan Mcdougall, Jul 28, 2003
    #5
  6. >> >You can't delete this on an automatic object. What does placement new
    >have
    >> >to do with anything?

    >>
    >> Nothing. Who said that?
    >>
    >>
    >> A sad Jonathan
    >>

    >
    >Quote '(assuming a placement new will be made)'.
    >
    >Just trying to understand your point, not pick an argument. Perhaps you
    >could illustrate with some code.


    I think irony does not travel very well through cable modems.

    I don't know why I said that, I even started an answer like

    class A
    {
    public:
    void f()
    {
    delete this;
    this =

    and then

    const_cast<A*>(this) =

    and worst

    const_cast<A*>(this) = new (this) A;

    before understanding the profound stupidity of my point. I then made
    a joke, hoping nobody would notice. Too bad :)

    Sorry about that,


    Jonathan
     
    Jonathan Mcdougall, Jul 28, 2003
    #6
    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. Replies:
    1
    Views:
    587
    Salt_Peter
    Dec 25, 2006
  2. WittyGuy
    Replies:
    2
    Views:
    557
    hurcan solter
    Aug 29, 2007
  3. Sandy Beech

    Only number input thru scanf()

    Sandy Beech, Nov 2, 2010, in forum: C Programming
    Replies:
    8
    Views:
    5,053
    David Thompson
    Nov 16, 2010
  4. THTB
    Replies:
    0
    Views:
    202
  5. Max Williams
    Replies:
    3
    Views:
    180
    Robert Klemme
    Jan 6, 2009
Loading...

Share This Page