Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 181875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:50:25+00:00 2026-05-11T14:50:25+00:00

after doing some test-code for this link : Is it safe to call temporary

  • 0

after doing some test-code for this link :
Is it safe to call temporary object's methods?
I found a rather strange feature of the c++ language, which I’m not sure how it works :

struct Test{     int i;     Test(int ii):i(ii){}     Test& operator=(int ii){         i = ii;         return *this;     }     Test operator+(const Test& rhs){         return Test(i + rhs.i);     }     void non_const_function(){         i *= i;     } };  int main(){         //code below gives errors, builtin types don't allow evil code         //int i = 5+5 = 8;         //int& iRef = 5+5;         //int* iPtr = &(5+5);         //5 = 10;          Test x = Test(5) + Test(5) = 8;//assign to a temporary         Test& xRef = Test(5) + Test(5);//reference to a temporary         Test* xPtr = &(Test(5) + Test(5));//address of a temporary         Test(5) = Test(10);//assign to a temporary         Test(8).non_const_function();//call a non-const function         return 0; } 

xRef and xPtr are both working pointers, with the expected values.
Of course I wouldn’t write such code in a real project, but I’m still interested how / why this works.
The only info I found on google about this was that ‘if you create a reference to a temporary, the temporaries lifetime is linked to the lifetime of the reference’

Note :
-not all compilers are that forgiving, e.g. Metrowerks ARM (does it use GCC ?) only allows const reference to temporaries.
EDIT :
-increasing the warning to W4 in VC++ 2008 showed lots of errors – good to know.

EDIT 2:
Thank you all for the help. I’m back to work, fixing 100’s of warnings.
CONCLUSION : use highest warning from start (I even found a REAL bug thanks to /G4)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:50:26+00:00Added an answer on May 11, 2026 at 2:50 pm

    Let’s go line by line:

        Test x = Test(5) + Test(5) = 8;//assign to a temporary 

    There’s no big deal here. Temporaries are still just normal objects, and thus the assignment operator works on them just as it would on anything else.

        Test& xRef = Test(5) + Test(5);//reference to a temporary 

    Like Metroworks, my GCC doesn’t allow a non-const reference to a temporary.

        Test* xPtr = &(Test(5) + Test(5));//address of a temporary 

    In addition, GCC warns about taking the address of a temporary, for obvious reasons.

       Test(5) = Test(10);//assign to a temporary 

    Again, this is just assignment, which, as I explained above, is no big deal.

        Test(8).non_const_function();//call a non-const function 

    Temporary objects aren’t constant. There’s nothing stopping them from calling non-const functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 225k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT b.pb_bank_code, b.pb.bank_name, b.total_amount, COALESCE(x.sum_amount, 0) FROM GLAS_PDC_BANKS b… May 13, 2026 at 1:05 am
  • Editorial Team
    Editorial Team added an answer All of them use the same transport protocol (HTTP). XMLRPC… May 13, 2026 at 1:05 am
  • Editorial Team
    Editorial Team added an answer The Frame class (and its child JFrame class for Swing)… May 13, 2026 at 1:05 am

Related Questions

I've seen the Testing iPhone app with limited network access ( link ) question,
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I'm writing an application for the iPhone that communicates with a SQLite database but
I've created an HttpModule in ASP.NET to allow users to upload large files. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.