Error passing object reference
Code:
#include <Data.h>
int main(int arg_count, char** argv)
{
if(arg_count!=3){
cerr<<"Files misisng\n";
exit(1);
}
Data object;
object.read_ref(argv[1]); //
object.read_mmp(argv[2]); //
std::map<std::string,Polymorphism> temp= object.ret_poly();
object.test(temp);// this works completely fine..
//object.test(object.ret_poly()); // this doesn't work
return 0;
}
Code:
class Data
{
public:
/** Default constructor */
Data();
void read_ref(const char* );
void read_mmp(const char*);
void test(std::map<std::string,Polymorphism>&); //some issue if I use object.test(object.ret_poly());
void set_poly(std::map<std::string, Polymorphism>&);
std::map<std::string, Polymorphism> ret_poly();
protected:
private:
std::map<std::string, Polymorphism> polymorphisms;
std::map<std::string, std::string> reference;
};
Data.cpp file
Code:
void Data::set_poly(std::map<std::string, Polymorphism>& t_poly){
polymorphisms=t_poly;
}
std::map<std::string, Polymorphism> Data::ret_poly(){
return(polymorphisms);
}
void Data::test(std::map<std::string,Polymorphism>& temp_poly){
}
//object.test(object.ret_poly());
This actually should work, because it is passing address of polymorphisms object.
I have tried changing prototype of test in Data.h, but failed.
Kindly help me understand, passing object address/pointers in C++.
Re: Error passing object reference
Does test need to change the map? If it doesn't then change its parameter to be a const reference. If it does change the map, then perhaps ret_poly should return a reference.
Re: Error passing object reference
Hi laserlight,
No, map doesn't need to be changed, I shall change it to const.
Can you please help me understand why //object.test(object.ret_poly()); fails? :(
Quote:
Originally Posted by
laserlight
Does test need to change the map? If it doesn't then change its parameter to be a const reference. If it does change the map, then perhaps ret_poly should return a reference.
Re: Error passing object reference
ret_poly returns a copy of the map, so when you have test's parameter be a non-const reference, the non-const reference parameter, not being an rvalue reference, cannot bind to the temporary object returned by the function. If it was a const reference then there's no problem as a const reference can be bound to such temporaries.
Another thing to consider: does test need to have a parameter at all? Maybe it should be a const member function that operates on the member variable directly. If you prefer for test to have a parameter, and it does not act on any member directly, then perhaps it should be a non-member non-friend function.
Re: Error passing object reference
Sorry didn't understand. Got lost in reference, bind. :(
I am noob in C++/C/pointer/reference world.
Re: Error passing object reference
Quote:
Originally Posted by
deathmetal
Sorry didn't understand. Got lost in reference, bind. :(
I am noob in C++/C/pointer/reference world.
So why the overly complex code? You get the same error here:
Code:
int foo()
{
return 10;
}
int main()
{
int& x = foo();
}
That is basically what your code is doing, and that is trying to assign a temporary value to a non-const reference. This doesn't work because foo() returns a temporary int, and you cannot assign a temporary to a non-const reference. If you could then this code would make sense:
Code:
int foo()
{
return 10;
}
int main()
{
int& x = foo();
x = 20;
}
The foo() returns a temporary, and that temporary disappears as soon as the first line finishes executing. So when you get to "x = 20", what is "x" referring to? Whatever it is, it is gone before the assignment of 10 is done. This is why you get the error.
Regards,
Paul McKenzie