Instantiation of an Object only thru "new Operator"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mysooru

    Instantiation of an Object only thru "new Operator"

    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;


  • John Harrison

    #2
    Re: Instantiation of an Object only thru "new Operator"


    "Mysooru" <nags_bhel@yaho o.com> wrote in message
    news:bg2f3h$djk $2@news.mch.sbs .de...[color=blue]
    > 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;[/color]
    it[color=blue]
    > should be AnObject obj = new AnObject;
    >[/color]

    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



    Comment

    • Jonathan Mcdougall

      #3
      Re: Instantiation of an Object only thru &quot;new Operator&quot;

      >> >How to restrict an object to be instantiated only using new operator?[color=blue][color=green][color=darkred]
      >> >
      >> >I mean, one should not be allowed to create an object like AnObject obj;[/color][/color]
      >it[color=green][color=darkred]
      >> >should be AnObject obj = new AnObject;[/color]
      >>
      >> And why would you want that?[/color]
      >
      >Perhaps because the object wants to do a "delete this;"?[/color]

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

      Jonathan

      Comment

      • John Harrison

        #4
        Re: Instantiation of an Object only thru &quot;new Operator&quot;


        "Jonathan Mcdougall" <DELjonathanmcd ougall@yahoo.ca > wrote in message
        news:qcj9ivcitp rp92r82bkb28osc 1tkuvnomv@4ax.c om...[color=blue][color=green][color=darkred]
        > >> >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[/color][/color][/color]
        obj;[color=blue][color=green]
        > >it[color=darkred]
        > >> >should be AnObject obj = new AnObject;
        > >>
        > >> And why would you want that?[/color]
        > >
        > >Perhaps because the object wants to do a "delete this;"?[/color]
        >
        > Isn't it allowed to do it even if it is an automatic object (assuming
        > a placement new will be made) ?
        >
        > Jonathan[/color]

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

        john


        Comment

        • Jonathan Mcdougall

          #5
          Re: Instantiation of an Object only thru &quot;new Operator&quot;

          On Mon, 28 Jul 2003 08:28:37 +0100, "John Harrison"
          <john_andronicu s@hotmail.com> wrote:
          [color=blue]
          >
          >"Jonathan Mcdougall" <DELjonathanmcd ougall@yahoo.ca > wrote in message
          >news:qcj9ivcit prp92r82bkb28os c1tkuvnomv@4ax. com...[color=green][color=darkred]
          >> >> >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[/color][/color]
          >obj;[color=green][color=darkred]
          >> >it
          >> >> >should be AnObject obj = new AnObject;
          >> >>
          >> >> And why would you want that?
          >> >
          >> >Perhaps because the object wants to do a "delete this;"?[/color]
          >>
          >> Isn't it allowed to do it even if it is an automatic object (assuming
          >> a placement new will be made) ?
          >>
          >> Jonathan[/color]
          >
          >You can't delete this on an automatic object. What does placement new have
          >to do with anything?[/color]

          Nothing. Who said that?


          A sad Jonathan

          Comment

          • Jonathan Mcdougall

            #6
            Re: Instantiation of an Object only thru &quot;new Operator&quot;

            >> >You can't delete this on an automatic object. What does placement new[color=blue]
            >have[color=green][color=darkred]
            >> >to do with anything?[/color]
            >>
            >> Nothing. Who said that?
            >>
            >>
            >> A sad Jonathan
            >>[/color]
            >
            >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.[/color]

            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

            Comment

            Working...