I wanted to know whether a(b) of type 'test' calls copy constructor or assignment operator, but i have got compilation error (mention inline).
I have declared geti() as public but still its giving error.. I am learning c++, can anybody explain me what those error and how to fix?
Thanks in advance
- Code: Select all
#include<iostream>
using namespace std;
class test
{
int i;
public:
test(int x=0):i(x){}
~test(){}
test(const test& y)
{
i = y.geti(); // error: passing ‘const test’ as ‘this’ argument of ‘int test::geti()’ discards qualifiers
}
test& operator=(test& m)
{
i = m.geti();
return *this;
}
int geti()
{
return i;
}
};
test sentobject(void)
{
test temp(88);
return temp;
}
int main()
{
test a(3),b(6);
cout<<"a(b)calls";
a(b); //no match for call to ‘(test) (test&)’
cout<<"a.i="<<a.geti()<<"b.i="<<b.geti()<<endl;
cout<<"sendobject calls"<<endl;
test k = sentobject();
return 0;
}
