CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2013
    Posts
    12

    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;
    
    }
    Data.h:
    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++.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2013
    Posts
    12

    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 View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Oct 2013
    Posts
    12

    Re: Error passing object reference

    Sorry didn't understand. Got lost in reference, bind.
    I am noob in C++/C/pointer/reference world.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error passing object reference

    Quote Originally Posted by deathmetal View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured