[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

returning refrence to void poiter

Posted on 2001-06-12
2
Medium Priority
?
369 Views
Last Modified: 2011-09-20
I have following peace of C++ Code which compiles & runs perfectly fine on Sun & Microsoft but will not compile on HP 11


/********************************************/
#include <iostream.h>
int i;
class cls1{

public:
    void*& func1(int a, int b) {

         void *x;
     
         i=a+b;
         x=&i;

         return (void*) x;
    }
};

int main(){

    cls1 obj1;

    int * b;

    b = (int *)obj1.func1(5,6);

    cout<<*b<<endl;

    return 0;
}


/**********************************************/
Error Given on HPUX
Error (future) 438: "cls1.cpp", line 22 # The initializer for a non-constant reference must be an lvalue.
Try changing 'void *&' to 'void *const &' at ["cls1.cpp", line 16].                                
                     
   return (void*) x;
                            ^    

Error 582: "cls1.cpp", line 22 # Initialization of the result of "void *&cls1::func1(int,int)" requires
creating a temporary, yet the temporary's  lifetime ends with the return from the function.        
                   
   return (void*) x;  
                            ^

/*********************************************/
What could be the possible reason? Also what, if any, changes are required to do in this code so that
it runs equally good on all three platforms?
0
Comment
Question by:avi_india
2 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6183358
The compiler doesn't realize that what you're returning (essentially  a reference to i) will still be in scope after the return.

Try changing the relevant code to simply:

void*& func1(int a, int b) {
        i=a+b;
        return (void *)i;
   }
0
 
LVL 5

Accepted Solution

by:
marecs earned 45 total points
ID: 6184980
Or

void* func1(int a, int b) {                                                    
   return (void*) new int(a+b);                                                                  
}                                                                              
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question