LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2007, 08:47 PM   #1
Millenniumman
Member
 
Registered: Apr 2006
Posts: 81

Rep: Reputation: 15
g++ not allowing returned objects as references


I am porting some code from Windows to Linux. I am starting to see a lot of problems relating to g++ not allowing returned objects to be passed as references. For example:

Code:
//functions
string alterString( string & inputString );
void doSomethingWithAlteredString(string & inputString);

//implementation
doSomethingWithAlteredString(alterString(aString));
With the above, doSomethingWithAlteredString will not work. g++ apparently will not accept the return of a function as a reference to an object, and complains that I am passing a string to function that takes a string&. Microsoft's compiler accepts this.

Is there any way to deal with this without making code changes wherever this occurs? Maybe a g++ compiler flag?

I know I could store the output of alterString in a variable, but this would require changes to a lot of code, and I don't see how I could do it in a function definition with default values.
 
Old 06-08-2007, 09:07 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You're trying to use the "const reference input object" optimization idiom, but forgot the const. You need the const because you're trying to push a temporary object into the method, and temporary objects are considered const.

Microsoft VC++ is once again very broken and non-compliant.
 
Old 06-08-2007, 10:56 PM   #3
Millenniumman
Member
 
Registered: Apr 2006
Posts: 81

Original Poster
Rep: Reputation: 15
I'll first note that I didn't write this code.

Second, where does the const go? The function definition? And this is the only way to solve this?

Thanks.
 
Old 06-08-2007, 11:43 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Code:
string alterString(const string & inputString );
void doSomethingWithAlteredString(const string & inputString);
 
Old 06-09-2007, 03:01 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
"Microsoft's compiler accepts this."
"Microsoft VC++ is once again very broken and non-compliant."

Did you try with language extensions turned off?
Although this normally means MS code will not compile
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Good References GuodMan Solaris / OpenSolaris 6 03-15-2006 08:31 PM
Security references unSpawn Linux - Security 6 02-25-2006 01:04 AM
??Good C References Please?? InvisibleSniper Programming 9 10-28-2005 01:45 PM
references jenny_psion Programming 0 08-12-2003 03:06 AM
references to other other sites pbharris LQ Suggestions & Feedback 10 04-10-2002 10:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration