object destruction

E

EN

Hi.

I've small problem :
I've two objects, each from another class:
example object a from class A and b from B.
I method from class A :
a.setValue() I made object b:
B *b = new B();

What is happen when in main program I invoke delete to object a,
object b is destroy to???

example:

int main(){
A *a = new A();
a->setValue(4);
delete a;
}

Thanks in advice
EN
 
S

Sharad Kala

EN said:
Hi.

I've small problem :
I've two objects, each from another class:
example object a from class A and b from B.
I method from class A :
a.setValue() I made object b:
B *b = new B();

What is happen when in main program I invoke delete to object a,
object b is destroy to???

example:

int main(){
A *a = new A();
a->setValue(4);
delete a;
}
Your language is not clear but b won't get destroyed when a is deleted. You
have to delete it explicitly.

Sharad
 
R

Rolf Magnus

EN said:
Hi.

I've small problem :
I've two objects, each from another class:
example object a from class A and b from B.

Your example code below only contains the former. Where is object b?
I method from class A :
a.setValue() I made object b:
B *b = new B();

What is happen when in main program I invoke delete to object a,
object b is destroy to???

Well, when you got it from new, it has to be deleted explicitly.
 
L

Leon Mergen

What is happen when in main program I invoke delete to object a,
object b is destroy to???

No, unless you specify it in a constructor. A common design practice I
use is delete an object in the same class as it is created. It avoids
confusion and keeps pointer management centralized.

Leon Mergen
http://www.solatis.com/
 

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

No members online now.

Forum statistics

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

Latest Threads

Top