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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:12:33+00:00 2026-06-03T16:12:33+00:00

I tried to understand how this allocation works in c++ : Test other =

  • 0

I tried to understand how this allocation works in c++ :

Test other = toto();

This is the full code source :

#include <iostream>

class Test
{
public:
    Test()
    {
        j = i++;
        std::cout<<"default constructor "<<j<<std::endl;
    }

    Test(const Test&)
    {
        std::cout<<"constuctor by copy "<<j<<std::endl;
    }
    Test & operator=(const Test&)
    {
        std::cout<<"operator = "<<j<<std::endl;
        return *this;
    }
    int j;
    static int i;
};

int Test::i = 0;

Test toto()
{
    Test t;
    return t;
}

int main()
{
    Test other = toto();
    std::cout<<other.j<<std::endl;
    Test another;
    return 0;
}

The code not used constructor by copy or operator =, so I don’t understand really how it’s works …
I used gcc 4.7.0

Thranks for your help 🙂

Jerome

  • 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-03T16:12:35+00:00Added an answer on June 3, 2026 at 4:12 pm

    The format semantics of:

    Test other = toto();
    

    involve several copies (but no assignment). The compiler is allowed to
    elide all of the different instances, however, which eliminates the
    copies; almost all compilers do do this optimization.

    More concretely, the standard doesn’t specify where values of class type
    are returned, but the usual solution is for the caller to allocate the
    space, and pass a hidden pointer to it into the function. Without the
    above mentionned optimizations:

    Test
    toto()
    {
        Test t;
        return t;
    }
    

    would result in the local variable t being constructed, then the
    return statement would copy t into the space pointed to by the hidden
    pointer. The optimization (called named return value optimization, or
    NRVO) here results in the compiler using the space pointed to by the
    hidden pointer for t, rather than creating a separate t locally.
    (Obviously, when it does this, it does not destruct t, as it would
    otherwise after the copy.)

    In the declaration:

    Test t = toto();
    

    , the formal semantics would have the compiler allocate the space for a
    temporary of type Test, pass the address of this space as the hidden
    pointer to toto, then copy this temporary into t and destruct it.
    The optimization here consists in the compiler passing the address of
    t directly to toto, eliding the intermediate temporary.

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

Sidebar

Related Questions

I tried to understand this line of code, but it is failed. $this->request->{self::FLAG_SHOW_CONFIG} ===
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
I have tried so many things but I don't understand this sooo confusing. I
Trying to understand this binding process of the WPF. See the code at the
I've tried to understand this, and searched SO for similar questions, but I still
I'm trying to understand this jquery code, I understand all of it: the modulo,
I don't understand this: I have tried to center the text-align of a HTML
I found this piece of code in perl system(zip $ZIP_DEBUG -r -9 itvlib.zip $include
I've tried to understand this post regarding this concept , however, I'm failing to
I can't understand this. When I want to remove class B from a div

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.