Deleting class instance from static member function?

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

    Deleting class instance from static member function?

    Is this acceptable?

    class CTest

    {

    public:

    static void selfDelete(CTes t *);

    int method() { }

    private:

    };

    void CTest::selfDele te(CTest *p)

    {

    delete p;

    }

    int main()

    {


    CTest *x = new CTest;

    x->selfDelete(x );

    return 0;

    }
    --
    Elias



  • jeffc

    #2
    Re: Deleting class instance from static member function?


    "lallous" <lallous@lgwm.o rg> wrote in message
    news:bleknh$bf0 f0$1@ID-161723.news.uni-berlin.de...[color=blue]
    > Is this acceptable?
    >
    > class CTest
    > {
    > public:
    > static void selfDelete(CTes t *);
    > int method() { }
    > private:
    > };
    >
    > void CTest::selfDele te(CTest *p)
    > {
    > delete p;
    > }
    >
    > int main()
    > {
    > CTest *x = new CTest;
    > x->selfDelete(x );
    > return 0;
    > }[/color]

    Sure, but it's a little misleading. It's not really a "self" delete. It's
    merely a delete function that does a delete on the pointer you send it. You
    could just as well write
    CTest* x = new CTest;
    int* p = new int;
    x->selfDelete(p );

    And you could just as easily take that selfDelete function out of the class
    and just call it Delete. See why it's misleading? This is much more
    interesting
    void selfDelete()
    {
    delete this;
    }


    Comment

    • lallous

      #3
      Re: Deleting class instance from static member function?

      "jeffc" <nobody@nowhere .com> wrote in message news:<3f7adf52_ 2@news1.prserv. net>...[color=blue]
      > "lallous" <lallous@lgwm.o rg> wrote in message
      > news:bleknh$bf0 f0$1@ID-161723.news.uni-berlin.de...[color=green]
      > > Is this acceptable?
      > >
      > > class CTest
      > > {
      > > public:
      > > static void selfDelete(CTes t *);
      > > int method() { }
      > > private:
      > > };
      > >
      > > void CTest::selfDele te(CTest *p)
      > > {
      > > delete p;
      > > }
      > >
      > > int main()
      > > {
      > > CTest *x = new CTest;
      > > x->selfDelete(x );
      > > return 0;
      > > }[/color][/color]
      [color=blue]
      >
      > Sure, but it's a little misleading. It's not really a "self" delete. It's
      > merely a delete function that does a delete on the pointer you send it. You
      > could just as well write
      > CTest* x = new CTest;
      > int* p = new int;[/color]
      [color=blue]
      > x->selfDelete(p );[/color]
      will that work? I mean even w/o casting the 'int *' to 'CTest *' ?

      [color=blue]
      > And you could just as easily take that selfDelete function out of the class
      > and just call it Delete. See why it's misleading? This is much more
      > interesting
      > void selfDelete()
      > {
      > delete this;
      > }[/color]
      how would 'this' exist if I have the selfDelete() outside of the class?


      Regards,
      Elias

      Comment

      • jeffc

        #4
        Re: Deleting class instance from static member function?


        "lallous" <lallous@lgwm.o rg> wrote in message
        news:3bf2b2cf.0 310011046.79585 207@posting.goo gle.com...[color=blue][color=green]
        > >
        > > Sure, but it's a little misleading. It's not really a "self" delete.[/color][/color]
        It's[color=blue][color=green]
        > > merely a delete function that does a delete on the pointer you send it.[/color][/color]
        You[color=blue][color=green]
        > > could just as well write
        > > CTest* x = new CTest;
        > > int* p = new int;[/color]
        >[color=green]
        > > x->selfDelete(p );[/color]
        > will that work? I mean even w/o casting the 'int *' to 'CTest *' ?[/color]

        It's true I forgot about the cast to int*, but yes, it would work. Since
        you're in a static function of CTest, you're not using anything specific to
        any object (instance) of CTest. It's basically just working like a regular
        function at that point (other than the fact that it's "inside" the class -
        in this sense it works like a namespace.) You don't really even need the x
        object or the x-> pointer notation to. You could just do this.

        int* p = new int;
        CTest::selfDele te( (CTest*)p );

        The point I was trying to make was that "self" is really misleading. When a
        function is static, there is no such thing as "self" or "this". (BTW, in
        the Smalltalk language, "self" is actually a term analogous to "this" in
        C++).
        [color=blue][color=green]
        > > And you could just as easily take that selfDelete function out of the[/color][/color]
        class[color=blue][color=green]
        > > and just call it Delete. See why it's misleading? This is much more
        > > interesting
        > > void selfDelete()
        > > {
        > > delete this;
        > > }[/color]
        > how would 'this' exist if I have the selfDelete() outside of the class?[/color]

        You're right, it would not. I wrote that as a quick snippet out of context.
        The context I should have written was

        class CTest
        {
        public:
        void selfDelete() { delete this; }
        };
        or alternatively
        void CTest::selfDele te { delete this; }


        Comment

        • lallous

          #5
          Re: Deleting class instance from static member function?

          "jeffc" <nobody@nowhere .com> wrote in message news:<3f7b30b2_ 3@news1.prserv. net>...[color=blue]
          > "lallous" <lallous@lgwm.o rg> wrote in message
          > news:3bf2b2cf.0 310011046.79585 207@posting.goo gle.com...[color=green][color=darkred]
          > > >
          > > > Sure, but it's a little misleading. It's not really a "self" delete.[/color][/color]
          > It's[color=green][color=darkred]
          > > > merely a delete function that does a delete on the pointer you send it.[/color][/color]
          > You[color=green][color=darkred]
          > > > could just as well write
          > > > CTest* x = new CTest;
          > > > int* p = new int;[/color][/color]
          >[color=green][color=darkred]
          > > > x->selfDelete(p );[/color]
          > > will that work? I mean even w/o casting the 'int *' to 'CTest *' ?[/color]
          >
          > It's true I forgot about the cast to int*, but yes, it would work. Since
          > you're in a static function of CTest, you're not using anything specific to
          > any object (instance) of CTest. It's basically just working like a regular
          > function at that point (other than the fact that it's "inside" the class -
          > in this sense it works like a namespace.) You don't really even need the x
          > object or the x-> pointer notation to. You could just do this.
          >
          > int* p = new int;
          > CTest::selfDele te( (CTest*)p );
          >
          > The point I was trying to make was that "self" is really misleading. When a
          > function is static, there is no such thing as "self" or "this". (BTW, in
          > the Smalltalk language, "self" is actually a term analogous to "this" in
          > C++).
          >[color=green][color=darkred]
          > > > And you could just as easily take that selfDelete function out of the[/color][/color]
          > class[color=green][color=darkred]
          > > > and just call it Delete. See why it's misleading? This is much more
          > > > interesting
          > > > void selfDelete()
          > > > {
          > > > delete this;
          > > > }[/color]
          > > how would 'this' exist if I have the selfDelete() outside of the class?[/color]
          >
          > You're right, it would not. I wrote that as a quick snippet out of context.
          > The context I should have written was
          >
          > class CTest
          > {
          > public:
          > void selfDelete() { delete this; }
          > };
          > or alternatively
          > void CTest::selfDele te { delete this; }[/color]

          Hello jeffc,

          The name of this function is meaningful from the real case I got and
          where I built this example from.

          I had a static method and inside it I manage to do something like:
          CTest *_this = GetThisForThisC lassInstanceFro mThisStaticFunc tion;
          delete _this;

          Also in Pascal language "Self" means "this"

          Regards,
          Elias

          Comment

          • jeffc

            #6
            Re: Deleting class instance from static member function?


            "lallous" <lallous@lgwm.o rg> wrote in message
            news:3bf2b2cf.0 310011938.16ab2 60d@posting.goo gle.com...[color=blue]
            >
            > The name of this function is meaningful from the real case I got and
            > where I built this example from.
            >
            > I had a static method and inside it I manage to do something like:
            > CTest *_this = GetThisForThisC lassInstanceFro mThisStaticFunc tion;
            > delete _this;
            >
            > Also in Pascal language "Self" means "this"[/color]

            In that case, I'm not sure why you (apparently) disagree that selfDelete is
            a misleading name for a static function that has no access to anything like
            "self", but whatever works for ya.....


            Comment

            Working...