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 8062237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:36:04+00:00 2026-06-05T10:36:04+00:00

class test{ public: int data; test(const test& ){cout<<INSIDE COPY CON <<endl;} test(int val =

  • 0
class test{

public:

    int data;
    test(const test& ){cout<<"INSIDE COPY CON "<<endl;}
    test(int val = 0) : data(val){ cout<<"INSIDE CON "<<endl; }

    test testfun(const test& obj)
    {
        cout<<"data : "<<data<<endl;
        //test test3(this->data + obj.data);
        //cout<<"test3 :"<<test3.data<<endl;
        //return test3;   //This will work only if return type is changed to const ref
        return test(data + obj.data); 


    }
};

int main()
{

    test testO1(1);
    test testO2(2);
    test testO3 = testO1.testfun(testO2);

    cout<<testO3.data<<endl;


    getchar();


}

OUTPUT:

INSIDE CON

INSIDE CON

data : 1

INSIDE CON

3

What happens when constructor is called in return statement? Since i am able to return by value and it works i think its not a temporary location. OR is it creating the object as a temporary and using copy constructor to copy the values , inthat case why is the print inside the copy constructor not getting printed.

  • 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-05T10:36:06+00:00Added an answer on June 5, 2026 at 10:36 am

    You ask a penetrating question.

    It is up to the compiler and the associated toolchain to decide what to do, but the basic pattern is as follows. Before calling testfun(), the caller [main(), in your example] reserves space for test03 on the stack. It then passes the address of test03 to testfun().

    The function testfun() must somehow put a test object at the address the caller has supplied. If the function has only one return statement, it is free to use the storage provided by the caller to build the return value. It need not use its own storage. It can use main()‘s.

    Now, this strategy does not always work. It typically fails when a function like testfun() has two, distinct return statements, one or both of which return not a temporary but a named object. In that case, the compiler is forced to do an otherwise unnecessary copy-on-return. However, the more usual case resembles yours, in which testfun() just builds the return value directly in the spot in which main() wants it. In this case, no actual copy occurs.

    It is thus up to the compiler to decide whether the copy constructor is called on return in a case like this.

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

Sidebar

Related Questions

class Test:ICloneable { int a; Test() { a = 20; } public int Data
say I have: class Test { public static int Hello = 5; } This
I have a class like this: public ref class Test { public: property int
Lets say i have this usercontrol public class test : UserControl { public int
I have an entity and its mapping: public class Test { public virtual int
class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT
I have the following class to test: public abstract class Challenge { protected int
heres my test in java public class person { public String name; public int
class test{ int x; public: //public functions getx() and putx() }; void main() {
This is my code: package test; import java.util.logging.Level; import java.util.logging.Logger; class Data{ int ar[]=new

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.