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

  • SEARCH
  • Home
  • 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 8343907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:12:29+00:00 2026-06-09T06:12:29+00:00

I compile the c++ code using the follow command to disable return value. g++

  • 0

I compile the c++ code using the follow command to disable return value.
g++ -fno-elide-constructors rvoptimazation.cpp -o test
But the output of ./test is
10
10
10
13
0xbfdf0020
13
I am confused by the last call of the constructor. Can anyone explain which line of the code will call the constructor after return in operator*? Thanks in advance.

#include<iostream>    

using namespace std;  

class Rational{  
public:  
    Rational(int x ,int y){  
        _a = x;  
        _b = y;  
        cout << __LINE__ << endl;  
    }  
    Rational(Rational const &t){  
        cout << __LINE__ << endl;  
    }  
    Rational operator*(Rational const &t){  
        Rational re = Rational(_a * t._a ,_b * t._b);  
        cout << &re << endl;  
        return re;  
        //return *this;  
    }  
    Rational get()  
    {  
        return *this;  
    }  
public:  
    int _a ,_b;  
};  

int main()  
{  
    Rational r1(1 ,2);  
    Rational r2(2 ,3);  
    r1 * r2;  
 // cout << &r3 << endl;  
}  
  • 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. Editorial Team
    Editorial Team
    2026-06-09T06:12:30+00:00Added an answer on June 9, 2026 at 6:12 am

    operator* returns by value, so the returned object must be constructed. The statement return re calls the copy constructor to do that.

    I think the explanation at Return value optimization is quite clear.

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

Sidebar

Related Questions

I am trying to compile my gcc code using the following make command .
Im trying to follow some python code using Ctrl +click, but Im trying to
How can you compile the code using javac in a terminal by using google-collections
I am trying to compile my piece of code using make. Normally I compile
I am trying to compile code that works on os x and linux using
I'm using Code::Blocks to compile a shared library on Ubuntu. When I make a
I am trying to compile an old C++ software project in Code::Blocks using the
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I'm struggling to compile some code that utilises the boost property tree. I'm using
I'm making a simple app using the HTML5 <canvas> tag and compile the code

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.