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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:22:51+00:00 2026-05-11T20:22:51+00:00

So I was writing some code, and I had something like this: class Box

  • 0

So I was writing some code, and I had something like this:

class Box
{
    private:
    float x, y, w, h;

    public:
    //...
    Rectangle & GetRect( void ) const
    {
        return Rectangle( x, y, w, h );
    }
};

Then later in some code:

Rectangle rect = theBox.GetRect();

Which worked in my debug build, but in release there were “issues” returning that Rectangle by reference — I basically got an uninitialized rectangle. The Rectangle class has an = operator and a copy constructor. Without getting into why this broke, I’m actually more interested in the correct way to return a (new) object by reference for the purpose of assigning copying to a variable. Am I just being silly? Should it not be done? I know I can return a pointer and then dereference on assignment, but I’d rather not. Some part of me feels like returning by value would result in redundant copying of the object — does the compiler figure that out and optimize it?

It seems like a trivial question. I feel almost embarrassed I don’t know this after many years of C++ coding so hopefully someone can clear this up for me. 🙂

  • 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-05-11T20:22:51+00:00Added an answer on May 11, 2026 at 8:22 pm

    You can’t return a reference to a temporary object on the stack. You have three options:

    1. Return it by value
    2. Return by reference via a pointer to something that you created on the heap with the new operator.
    3. Return by reference what you received by reference as an argument. [EDIT: Thanks to @harshath.jr for pointing this out]

    Note that when you return by value as in the code below, the compiler should optimize the assignment to avoid the copy – i.e. it will just create a single Rectangle (rect) by optimizing the create+assign+copy into a create. This only works when you create the new object when returning from the function.

    Rectangle GetRect( void ) const
    {
        return Rectangle( x, y, w, h );
    }
    
    Rectangle rect = theBox.GetRect();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • 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 Oracle will not compile your definition because the type Item… May 11, 2026 at 10:30 pm
  • Editorial Team
    Editorial Team added an answer Keeping track of the current position yourself is the most… May 11, 2026 at 10:30 pm
  • Editorial Team
    Editorial Team added an answer Why doesn't your controller know about the ObjectContext? This is… May 11, 2026 at 10:30 pm

Related Questions

The first real software company that I worked at was all about the unit
Background In a C# command-line app I'm writing, several of the parameters have yes
I'm currently writing an ASP.Net app from the UI down. I'm implementing an MVP
I recently took a class at school where we had to learn Scheme to
I have an IE BHO that's in development in C#. It is supposed to

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.