473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Deleting class instance from static member function?

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;

}
--
Elias
http://lgwm.org/
Jul 19 '05 #1
5 3615

"lallous" <la*****@lgwm.org> wrote in message
news:bl************@ID-161723.news.uni-berlin.de...
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;
}
Jul 19 '05 #2
"jeffc" <no****@nowhere.com> wrote in message news:<3f********@news1.prserv.net>...
"lallous" <la*****@lgwm.org> wrote in message
news:bl************@ID-161723.news.uni-berlin.de...
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); 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
Jul 19 '05 #3

"lallous" <la*****@lgwm.org> wrote in message
news:3b**************************@posting.google.c om...

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 *' ?


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++).
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?


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; }
Jul 19 '05 #4
"jeffc" <no****@nowhere.com> wrote in message news:<3f********@news1.prserv.net>...
"lallous" <la*****@lgwm.org> wrote in message
news:3b**************************@posting.google.c om...

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 *' ?


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++).
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?


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
Jul 19 '05 #5

"lallous" <la*****@lgwm.org> wrote in message
news:3b**************************@posting.google.c om...

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.....
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nobody You Know | last post by:
I need a member variable in class A known within an instance of class B, even though B does not instantiate A. My solution was to make the member variable private static, and create a public...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.