473,408 Members | 2,888 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,408 software developers and data experts.

opeator new question: again!

Here is snippet code.
class B contains class A and A1 as data member. when client code execute new
B(), B construtor throw exception, ...
C++ will call destrcutor of A and A1 seperately. This is very confusing.
Since A1 and A are also on heap. How C++ track dow these member? Thanks!

#include <stdio.h>
#include <memory>
using namespace std;

class O
{
public:
O(){};
~O(){};
}
;
class A1
{
O* o;
public :
A1()
{
o=new O();
printf("in A1 ctor\n");
}
~A1()
{
delete o;
printf("in A1 dtor\n");
};
};

class A
{
public :
A(){printf("in A ctor\n");};
~A(){printf("in A dtor\n");};
};
class B
{

auto_ptr<A> a;
A1 a1;
public:
B():a(new A)
{
throw 1;
}
~B(){printf("in B dtor\n");}
};
int main(int argc, char* argv[])
{
try{
B *b=new B();
}
catch(int)
{
printf("catch block\n");
}
return 0;
}
After running a1 and a destructor will be called. This confused me. Since b
is created in heap, and a and a1 is also on heap, how come exception can
call a1 and a's destructor.

Thanks for furthermore clarifying.

Best regards to all!

Jul 22 '05 #1
2 1148
john sun wrote:
Here is snippet code.
class B contains class A and A1 as data member. when client code execute new
B(), B construtor throw exception, ...
C++ will call destrcutor of A and A1 seperately. This is very confusing.
Since A1 and A are also on heap. How C++ track dow these member? Thanks!

#include <stdio.h>
#include <memory>
using namespace std;

class O
{
public:
O(){};
~O(){};
}
;
class A1
{
O* o;
public :
A1()
{
o=new O();
printf("in A1 ctor\n");
}
~A1()
{
delete o;
printf("in A1 dtor\n");
};
};

class A
{
public :
A(){printf("in A ctor\n");};
~A(){printf("in A dtor\n");};
};
class B
{

auto_ptr<A> a;
A1 a1;
public:
B():a(new A)
{
throw 1;
}
~B(){printf("in B dtor\n");}
};
int main(int argc, char* argv[])
{
try{
B *b=new B();
}
catch(int)
{
printf("catch block\n");
}
return 0;
}
After running a1 and a destructor will be called. This confused me. Since b
is created in heap, and a and a1 is also on heap, how come exception can
call a1 and a's destructor.
...


For 'a1' it doesn't really matter whether it is "on heap" or on
something else. 'a1' is a member subobject of class 'B'. In other words,
'a' is not a standalone object, it is an integral part of 'B' object, it
is embedded in 'B' object. This immediately means that its destructor
will be called in this situation. Destructors for _all_ immediate
subobjects of class 'B' will be called if 'B::B()' throws an exception
the way it is thrown in your code.

This also applies to 'a', which is an 'auto_ptr' subobject of class 'B'.
'a's destructor ('auto_ptr::~auto_ptr') is called, which in turn
'delete's the 'A' object dynamically allocated in the constructor
initialization list.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2

"john sun" <jo*****@sbcglobal.net> skrev i en meddelelse
news:VF******************@newssvr31.news.prodigy.c om...
Here is snippet code.
class B contains class A and A1 as data member. when client code execute new B(), B construtor throw exception, ...
C++ will call destrcutor of A and A1 seperately. This is very confusing.
Since A1 and A are also on heap. How C++ track dow these member? Thanks!

#include <stdio.h>
#include <memory>
using namespace std;

class O
{
public:
O(){};
~O(){};
}
;
class A1
{
O* o;
public :
A1()
{
o=new O();
printf("in A1 ctor\n");
}
~A1()
{
delete o;
printf("in A1 dtor\n");
};
};

class A
{
public :
A(){printf("in A ctor\n");};
~A(){printf("in A dtor\n");};
};
class B
{

auto_ptr<A> a;
A1 a1;
public:
B():a(new A)
{
throw 1;
}
~B(){printf("in B dtor\n");}
};
int main(int argc, char* argv[])
{
try{
B *b=new B();
}
catch(int)
{
printf("catch block\n");
}
return 0;
}
After running a1 and a destructor will be called. This confused me. Since b is created in heap, and a and a1 is also on heap, how come exception can
call a1 and a's destructor.

Thanks for furthermore clarifying.

Best regards to all!

When an exception in a constructor causes an object not to be created, all
members that were constructed will be destroyed automatically. Have a look
at B's constructor:

B():a(new A)
{
throw 1;
}

Since all members not explicitly constructed are default-constructed, this
code really corresponds to

B():a(new A),a1()
{
throw 1;
}

so both a and a1 are constructed at entry to the constructor body.
/Peter
Jul 22 '05 #3

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

Similar topics

33
by: John Timbers | last post by:
I'd like to purchase Visual C# .Net for learning purposes only since it's a lot cheaper than Visual Studio (note that I'm a very experienced C++ developer). Can someone simply clarify the basic...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
7
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
35
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
4
by: George2 | last post by:
Hello everyone, In a class T, if there is a conversion opeator G, we always define it like this, operator G() without return type. So the implicit return type is G or G&? I can not find it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.