Deleting class instance from static member function?

L

lallous

Is this acceptable?

class CTest

{

public:

static void selfDelete(CTest *);

int method() { }

private:

};

void CTest::selfDelete(CTest *p)

{

delete p;

}

int main()

{


CTest *x = new CTest;

x->selfDelete(x);

return 0;

}
 
J

jeffc

lallous said:
Is this acceptable?

class CTest
{
public:
static void selfDelete(CTest *);
int method() { }
private:
};

void CTest::selfDelete(CTest *p)
{
delete p;
}

int main()
{
CTest *x = new CTest;
x->selfDelete(x);
return 0;
}

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;
}
 
L

lallous

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);
will that work? I mean even w/o casting the 'int *' to 'CTest *' ?

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;
}
how would 'this' exist if I have the selfDelete() outside of the class?


Regards,
Elias
 
J

jeffc

lallous said:
will that work? I mean even w/o casting the 'int *' to 'CTest *' ?

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::selfDelete( (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++).
how would 'this' exist if I have the selfDelete() outside of the class?

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::selfDelete { delete this; }
 
L

lallous

jeffc said:
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::selfDelete( (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++).


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::selfDelete { delete this; }

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 = GetThisForThisClassInstanceFromThisStaticFunction;
delete _this;

Also in Pascal language "Self" means "this"

Regards,
Elias
 
J

jeffc

lallous said:
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 = GetThisForThisClassInstanceFromThisStaticFunction;
delete _this;

Also in Pascal language "Self" means "this"

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.....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top