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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:48:10+00:00 2026-06-10T23:48:10+00:00

Say I have the following code: #include <iostream> using namespace std; class Account {

  • 0

Say I have the following code:

#include <iostream>

using namespace std;

class Account
{
private:
    float balance;

public:
    Account() { balance = 0.0; };
    float GetBalance() { return balance; };
    void SetBalance(float newBalance) { balance = newBalance; };
};

Account mainAccount;

Account& GetAccount()
{
    return mainAccount;
}

void PrintAccountInfo()
{
    cout << "mainAccount's balance is " << mainAccount.GetBalance() << endl;
}

int main()
{
    PrintAccountInfo();
    Account &a = GetAccount(); // Line 31
    a.SetBalance(30.0);
    PrintAccountInfo();
    return 0;
}

When I run it, I get the following output (as expected):

mainAccount's balance is 0
mainAccount's balance is 30

However, on line 31, if I take out the "&" in the "Account &a", to make it this:

Account a = GetAccount(); // note lack of "&"

I get this output:

mainAccount's balance is 0
mainAccount's balance is 0

How come? I thought when returning a reference, the "&" is redundant / not necessary? Am I fundamentally misunderstanding how references work in C++?

EDIT: Thanks, I understand now why the two are different. However, then shouldn’t I be able to do this:

Account GetAccount()
{
    return mainAccount;
}

int main()
{
    Account &a = GetAccount();
    // ....
}

However, when I run that, I get an error:

untitled: In function ‘int main()’:

untitled:31: error: invalid initialization of non-const reference of type ‘Account&’ from a temporary of type ‘Account’

  • 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-10T23:48:12+00:00Added an answer on June 10, 2026 at 11:48 pm

    I thought when returning a reference, the “&” is redundant / not necessary?

    You thought wrong.

    Consider these two different lines:

    Account &a = GetAccount(); // Line 31
    
    Account a = GetAccount(); // Line 31
    

    In the first, you declare a reference called a which is bound to the object returned by the function GetAccount.

    In the second, you declare an object a which is copy-initialized by the object returned by the function GetAccount.

    Fundamentally: one declares a reference, the other declares an object.


    EDIT: Answering the follow-on question:

    “can I remove the & from the return type in the declaration of the GetAccount function: Account GetAccount() { return mainAccount; }“

    You certainly can remove the &, but then your behavior will change. Consider these two functions:

    Account GetAccount() { return mainAccount; }
    
    Account &GetAccount() { return mainAccount; }
    

    In the first, you return a temporary object which has been copy-initialized from the mainAccount object. In the second you return a reference to the mainAccount object.

    • If you want a to be a reference to mainAccount, you need & in both places.

    • If you want a to be a copy of mainAccount, you need no & in the declaration of a. The other declaration won’t matter in this case.

    • If you want a to be a reference to a compiler-generated temporary value (hint: you don’t), declare a with &, but GetAccount without.

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

Sidebar

Related Questions

Say I have the following code: #include <string> using namespace std; string GetPath(const string
Lets say we have the following code: #include <iostream> #include <string> struct A {
Say I have the following code: class Parent { static string MyField = ParentField;
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
Let's say we have the following code in a class: //class code TextBox t
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
Say I have a class, something like the following; class MyClass { public: MyClass();
Assume I have the following code: public static class Foo { public static void
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Say I have the following code: <span>Hello world!</span> And the following CSS: span{ color:red;

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.