#<include<iostream.h>
#include<conio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
counter(int c)
{
count=c;
}
void show()
{
cout<<count<<endl;
}
counter operator++();
};
counter counter::operator++()
{
counter temp;
++count;
temp.count=count;
return(temp);
}
void main()
{
clrscr();
counter c1(15);
counter c2;
c1.show();
c2.show();
c2=++c1;
c1.show();
c2.show();
getch();
}

my question is that in line no 26 i.e, return(temp); will compile and run easily but when we write return(count); it will also run easily why? as the return type of function is counter not int then also return(count); it did not give any error ... please give me an appropriate answer.

return(count) invokes the constructor that takes an int, so you get a newly constructed counter object using count as the initializer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.