Friday, 9 August 2013

Why does destructor get called in the following code?

Why does destructor get called in the following code?

Consider the following code. This code is taken from the book Object
Oriented Programming With C++!-Chapter 12.Templates.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
class vc
{
int size2;
int *v;
public :
vc(int size1);
vc(int *a);
~vc()
{
printf("\n calling destructor");
}
int operator *(vc );
};
vc::vc(int size1)
{
v = new int[size2=size1];
for(int i=0;i<this->size2;i++)
v[i]=0;
}
vc::vc(int a[])
{
for(int i=0;i<this->size2;i++)
{
v[i]=a[i];
}
}
int vc::operator *(vc v1)
{
int total=0;
for(int i=0;i<size2;i++)
total+=(v[i]*v1.v[i]);
return total;
}
int main()
{
int a[3]={1,2,3};
int b[3]= {5,6,7};
vc v1(3),v2(3);
v1=a;
v2=b;
int total = v1*v2;
cout << total;
return 0;
}
First of all this code is not working properly. It should show 38 as
output. When I started debugging this code, I found 3 is assigned to size2
after this line vc v1(3),v2(3);. But while executing the next line,
control is passed to the second constructor and size2 shows a garbage
value. Moreover,destructor is called after line v1=a and same happens
after the next line.
Final output:
calling destructor
calling destructor
calling destructor0
Why does destructor get called 3 times? Is this code wrong?

No comments:

Post a Comment