Solved

returning refrence to void poiter

Posted on 2001-06-12
2
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 15 total points
ID: 6184980
Or

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

705 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