cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : ORDER IN WHICH OBJECTS ARE DESTROYED
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  ORDER IN WHICH OBJECTS ARE DESTROYED

G SATISH KUMAR (16)
class a{---------
---------};
int main(){
{a q,w,e;
}
}

doubt:in the above program object 'q' is created first then 'w' and later 'e' is created.
when cursor moves out of block in main in which 3 objects are created,destructor is called automatically and destroys objects in the reverse order in which they are created.that is 'e' is destroyed first then 'w' is destroyed and then 'q' is destroyed.
but how can we confirm that objects are destroyed in the reverse order in in which they are created??????
please write a program to confirm the above discussion.
|
Zaita (1561)
1
2
3
4
5
6
7
8
9
10
11
12
class A {
 A() { };
 ~A() { cout << "A Is being destroyed" << endl; }
};

// etc

int main() {
 A myA = A();
 B myB = B();
 // etc
}


I would note. I prefer to work with Pointers to dynamically allocated objects. This allows me to control (and forget) the deallocation of memory.
| Last edited on

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us