class abc
{ int x;
public:
开发者_如何学Cabc(){}//default constructor
abc(int a)
{
x=a;
}
abc operator++(int a)//code for postfix ++ overloading
{
a=x++;
return a;
}
void display()
{
cout<<x<<endl;
}
};
int main()
{
abc a(12),q;
q=a++;
q.display();
return 0;
}
//code gives no compiling error and working well
The only "question" I can see in here is the return a;
because a
is an int
and the method is declared to return an abc
. In this case, the compiler will invoke the abc(int)
constructor to construct a new instance of the object for return from the increment operator.
If you change the constructor to be explicit
:
explicit abc(int a)
{
x=a;
}
then you will get an error:
t.cpp: In member function 'abc abc::operator++(int)': Line 12: error: conversion from 'int' to non-scalar type 'abc' requested compilation terminated due to -Wfatal-errors.
精彩评论