I am trying to solve a quiz and came across this question.
Please explain what happens internally that leads to this Outputclass B; class A { friend class B; public: ~A() { B boj(); cout << "object A destructor " << endl; } }; class B { public: ~B() { cout << "object B destructor " << endl; } }; int main() { A a; A aobj(); B bobj(); }
The output is:
Object A destructor
I am trying to create a program where a user enters if he wishes to add another record, and if yes then create a new object for that record.
So if I am including constructors, then how do I create a new object every time the user wants?
(If I give a predefined size to the array of object, then constructor will be called, say 50 times and initialize all 50 objects, while the user may only want to enter less).
Asked
Active
Viewed 126 times
0

GSerg
- 76,472
- 17
- 159
- 346

Abhishek Wadhawan
- 13
- 3
-
1One question per question, please. Which one would you like us to answer? – Lightness Races in Orbit Nov 08 '14 at 21:39
-
@TheParamagneticCroissant: Yes, I already said that in my answer. – Lightness Races in Orbit Nov 08 '14 at 21:42
-
@LightnessRacesinOrbit that was not directed towards you but towards OP. I was typing my comment while you were composing your answer. – The Paramagnetic Croissant Nov 08 '14 at 21:42
1 Answers
0
First, a
is constructed. Then, a
is destroyed.
The final two declarations in main
, and the declaration inside ~B()
, are all local function declarations and therefore don't "do" anything.

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055