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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:24:51+00:00 2026-06-01T20:24:51+00:00

If we return pointers from functions, we can write either const X* f(); or

  • 0

If we return pointers from functions, we can write either

const X* f();

or

X* const f();

,in this way controlling whether the pointer can be reassigned or if the class can be modified internally. But when returning references, these two seems to have the same meaning:

const X& f();
X& const f();

It seems impossible to return a reference where you can modify X, but not re-assign it? And if it is indeed impossible, why should we ever return references when pointers seem potent in this area?

UPDATE: As been pointed out, references cannot be reassigned. However this makes me even more bewildered, as the following code prints 33 55, not 33 33 as I would expect. How does that match with that references cannot be reassigned?

struct X
{
   X(int i_) { i = i_;} 
   int i;
};

struct Y
{
    X& get2() {tmp2 = new X(55); return *tmp2;} 
    X& get() {tmp = new X(33); return *tmp;}    
    void print () {cout << tmp->i << endl;}
    X* tmp;
    X* tmp2;
};

int _tmain(int argc, _TCHAR* argv[])
{
    Y y;    
    X& tmp2 = y.get2();
    X& tmp = y.get();       
    y.print();
    tmp = tmp2;
    y.print();
    return 0;
} 
  • 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-01T20:24:51+00:00Added an answer on June 1, 2026 at 8:24 pm

    It seems impossible to return a reference where you can modify X, but not re-assign it?

    References can never be re-assigned. They keep referring the same referent to which they are bound at initialization.

    why should we ever return references when pointers seem potent in this area?

    Reference usage is more intuitive, the caller of functions can use references simply like variables unlike pointers where the user has to deal with the dereferencing etc.

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

Sidebar

Related Questions

I'm getting a warning: Return makes pointer from integer without a cast for this
I often use convenience functions that return pointers to static buffers like this: char*
I have argument with my friend. He says that I can return a pointer
I can make a file pointer write to a file with fopen(). But can
my JavaCompiler returns a null pointer when called from a jar file. Everything works
GCC compiles (using gcc --omit-frame-pointer -s ): int the_answer() { return 42; } into
Can I use a native method which returns a pointer? I used the following
I've got a conversion operator that returns a const pointer, and I need to
This is in C++. So, I'm starting from scratch writing a game engine for
I am trying to learn how to call this write_data(…) function from the funmain()

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.