-
November 23rd, 2013, 11:41 PM
#1
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++.
-
November 24th, 2013, 12:17 AM
#2
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.
-
November 24th, 2013, 01:01 AM
#3
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? 
 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.
-
November 24th, 2013, 01:37 AM
#4
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.
-
November 24th, 2013, 02:58 PM
#5
Re: Error passing object reference
Sorry didn't understand. Got lost in reference, bind. 
I am noob in C++/C/pointer/reference world.
-
November 24th, 2013, 05:14 PM
#6
Re: Error passing object reference
 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
Last edited by Paul McKenzie; November 24th, 2013 at 05:16 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|