B
bipod.rafique
Hello All,
I need your help in understanding something.
I have a simple class
class test{
};
And I also have a simple insertion operator:
ostream& operator<<(ostream& out, test &a){
out<<"testing";
}
(it is just for testing.)
Note that I do not have const reference to the "test" object in the
insertion operator function.
Now from another function (say main), I have this
int main(){
test a
cout<<a<<endl;
return 0;
}
It works fine, and I get "testing" as output.
But if I have a function say:
test f(){
test a;
return a;
}
[this returns a test object]
and have my main as this:
int main(){
cout<<f()<<endl;
return 0;
}
I get errors!!!, and top errors says I don't have a matching operator<<
in std:cout<<f()()
Now what is that? and why is that?
To fix it, I pass the test object in insertion operator as *const*
reference. It all works. Even if I pass test object by value, its
works!.
So whats the deal? Is there something special when returing an object
from a function?
BTW: I do send objects as const references whenever possible, but why
should it be *necessary* to send as const?
Thanks
Bipod
I need your help in understanding something.
I have a simple class
class test{
};
And I also have a simple insertion operator:
ostream& operator<<(ostream& out, test &a){
out<<"testing";
}
(it is just for testing.)
Note that I do not have const reference to the "test" object in the
insertion operator function.
Now from another function (say main), I have this
int main(){
test a
cout<<a<<endl;
return 0;
}
It works fine, and I get "testing" as output.
But if I have a function say:
test f(){
test a;
return a;
}
[this returns a test object]
and have my main as this:
int main(){
cout<<f()<<endl;
return 0;
}
I get errors!!!, and top errors says I don't have a matching operator<<
in std:cout<<f()()
Now what is that? and why is that?
To fix it, I pass the test object in insertion operator as *const*
reference. It all works. Even if I pass test object by value, its
works!.
So whats the deal? Is there something special when returing an object
from a function?
BTW: I do send objects as const references whenever possible, but why
should it be *necessary* to send as const?
Thanks
Bipod