FuncA is called to delete itself with B's help.
In vc 7.0, case 1 seems invalid, but case 2 is valid.
Could anyone explain for me?
class A
{
public:
void FuncA()
{
B b(this);
};
void FuncB()
{
delete this;
};
}
class B
{
public:
B(A* pThis): m_pThis(pThis){ };
//case 1
~B()
{ m_pThis.FuncB() ;}
//case 2
~B()
{delete pThis;}
}
~ Let us linux ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =----- 5 1643
Yongming Wang wrote: FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me?
.... //case 1 ~B() { m_pThis.FuncB() ;}
//case 2 ~B() {delete pThis;}
I suspect you mean "delete m_pThis" ?
}
These (case 1 & 2 ) destructors do exactly the same thing and there is
nothing invalid about either of them.
Perhaps you could explain why you think they're different ?
"Yongming Wang" <no****@126.com > wrote in message
news:m9******** *************** *********@4ax.c om... FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me?
class A { public: void FuncA() { B b(this); }; void FuncB() { delete this; }; }
class B { public: B(A* pThis): m_pThis(pThis){ };
//case 1 ~B() { m_pThis.FuncB() ;}
Assuming that m_pThis is A*?
This should be m_pThis->FuncB();
//case 2 ~B() {delete pThis;}
did you mean m_pThis?
Hi, sorry for my 2 mistakes.
It should be m_pThis->FuncB() and delete m_pThis.
But It is no effection with my problem.
I found the case 1 incorrect because when I do this, the CPU usage is
abnormally high to nearly 100%. But if I use case 2, all is OK.
Now I think the reason is FuncA called FuncB in it's body, so FuncB is
called multitimes. Call "delete this" in the class member function is
not a good design.
Thank you all.
In article <m9************ *************** *****@4ax.com>,
Yongming Wang <no****@126.com > wrote: FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me?
class A { public: void FuncA() { B b(this); }; void FuncB() { delete this; }; }
class B { public: B(A* pThis): m_pThis(pThis){ };
//case 1 ~B() { m_pThis.FuncB() ;}
//case 2 ~B() {delete pThis;} }
It is always best to post compiled code that demonstrates the problem
you are having, or if the code won't compile, then show us the code and
the error messages you are getting. I took the liberty of doing this for
the above:
class A
{
public:
void FuncA();
void FuncB() { delete this; }
};
class B
{
A* m_pThis;
public:
B(A* pThis): m_pThis(pThis){ }
// comment out one or the other d_tor
//case 1
~B() { m_pThis->FuncB();}
//case 2
//~B() {delete m_pThis;}
};
void A::FuncA()
{
B b(this);
}
int main()
{
A* a( new A );
a->FuncA();
}
In both cases, the program exits normally (at least on my system.) Why
is it that you think one of the d_tor's isn't valid? What is the message
you are getting from the compiler/run-time system? no****@126.com (Yongming Wang) wrote: Hi, sorry for my 2 mistakes. It should be m_pThis->FuncB() and delete m_pThis. But It is no effection with my problem. I found the case 1 incorrect because when I do this, the CPU usage is abnormally high to nearly 100%. But if I use case 2, all is OK. Now I think the reason is FuncA called FuncB in it's body, so FuncB is called multitimes.
So FuncA would create a B object (passing a pointer to itself,) then
delete itself, then the B object would get destroyed which would call a
member function on a deleted object (with d_tor 1) or would delete the A
object a second time (with d_tor 2).
Either way, both cases are in error. Maybe you should post your code?
Call "delete this" in the class member function is not a good design.
Not generally, but sometimes it is exactly what you need. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Stevens |
last post by:
Probably the wrong wording but since I'm not a scripter I won't claim
to know what I'm talking about.
I got this script from www.htmlgoodies.com
<script language="JavaScript">
<!--
window.open ('photos01.html','photogallery',config='height=550,
width=750,toolbar=no, menubar=no, scrollbars=no, resizable=no,
location=no, directories=no, status=no');
|
by: Stefan Strasser |
last post by:
why is delete an expression and not a statement? (in my draft copy of
the standard).
I was about to ask the same question about "throw" but found an
expression case of throw("return boolvalue ? 5 : throw 5;").
but "delete" neither does exit the scope nor has a return value.
any idea?
thanks,
|
by: niyong |
last post by:
--
Hop@Ni
|
by: lawrence |
last post by:
I'm trying to gain a better understanding of javascript by studying
examples. I noticed this in an online tutorial. I don't get the use of
"this".
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
You might want to add some event handler for an event to an element to
which there is already an event handler attached. That is possible by
storing the old event handler somewhere and then writing a new
function
which calls the old event handler.
|
by: R.Z. |
last post by:
i'm using a class from some api that is said to automatically call its
destructor when its out of scope and deallocate memory. i create instances
of this class using "new" operator. do i have to explicitly call delete on
these instances when i no longer need them?
| |
by: Player |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello all.
I am in the process of teaching myself C# and I think I am doing OK.
I have learnt how to how to call the right constructor of a class, if the
class has more than than one cosntructor, by making sure that each
constructor has a different signature. I have managed to learn that and get
|
by: Dave |
last post by:
I'm never quite sure whether to use "this." or not when referring to fields
or properties in the same class. It obviously works just fine without it
but sometimes I wonder if using this. consistently may make the code easier
to understand for someone else, etc. Using "this." makes it immediately
clear, for example, that the variable being referred to is a member of the
same class and is not declared in the method as a local variable.
...
|
by: Terry Olsen |
last post by:
I need to delete files from "C:\WINNT\Downloaded Program Files"
I can delete them using Windows Explorer, but they do not appear in the
command prompt directory listing.
How can I delete these items using VB?
*** Sent via Developersdex http://www.developersdex.com ***
|
by: WaterWalk |
last post by:
Hello. The question about "deleting this inside the class's member
function" is discussed many times in this group and in the "C++
FAQs". But I still have two more questions.
1. Take the following class as an example:
class Test
{
public:
static void print_message(char *s) { printf("%s\n", s); }
void delete_me()
|
by: Cirene |
last post by:
Can you assist me with this database problem?
I have 4 tables in my db....
Table1 (key: Table1Id)
Table2 (key: Table2Id)
Table3 (key: Table3Id)
Table 4 has these fields:
key: Table4Id
Table1Id (ties it to Table 1)
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |