Constructor and Destructor

Hey guys can any one tell what's happening in this code :
1
2
3
4
5
6
7
class student {
public : int static s ;
student( ){cout<<++s ;}
~student( ){cout<<--s ;} };
int student ::s = 0;
void main ( )
{ student s1; student s2(s1); }   //output 1 0 -1 

which means it's calling the constructor then the destructor 2 times , why ?
You create student s1, so the default constructor is used.
Then, you create s2 by copying s1, so the copy constructor is called, and s is unchanged.
Then, s2 and s1 are both destroyed, so the destructor is called twice.
But there's no copy constructor in the class ! or it's a default one built in ??
billy29 wrote:
it's a default one built in


Bingo.

http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
Topic archived. No new replies allowed.