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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:01:30+00:00 2026-06-01T15:01:30+00:00

Possible Duplicate: C++: When to use References vs. Pointers I’m pretty new programmer to

  • 0

Possible Duplicate:
C++: When to use References vs. Pointers

I’m pretty new programmer to the C/C++ languages and since I am coming from a background of C#, Java, JavaScript and a little bit of Visual Basic and Python, I am having a hard time to understand some of the things in C++.

I already know how to use reference and pointers and what they really mean, etc. But I don’t understand why and where to use them. I know that references sometimes are used like this:

int x = 2;

void Square(int &value)
{
   value = value*value;
}

Square(x);

cout << x << endl;

And the output will be 4.

I thought I don’t quite understand why to do it that way and not do like this:

int x = 2;

int Square(int value)
{
   value = value*value;
   return value;
}

x = Square(x);

cout << x << endl;

Anyway, I hope someone may be able to help me understand why and where to use reference and pointers.

  • 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-01T15:01:32+00:00Added an answer on June 1, 2026 at 3:01 pm

    You pass references for two main reasons:

    • to change the value of the object referenced inside your function
    • to avoid the cost of copying the object you pass

    If you want the second benefit but not the first, you usually pass a const reference to avoid mistakenly changing the object passed.

    In C++, pointers and references are much the same with two exceptions:

    • A reference cannot point to 0 (or nullptr)
    • A reference must always point to the same object

    If you want to do one of these things, you have to use a pointer.

    Additionally, pointers usually point to objects created in the heap by using new, while references usually point to objects created in the stack. This means that the lifetime of objects pointed to by pointers is usually controlled manually. You have to explicitly call delete on objects created by new in order to destroy them, but the lifetime of objects created in the stack is automatically managed by the runtime (hence their name “automatic variables”).

    I don’t know if this is all you needed to know or you are looking for something more specific.

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

Sidebar

Related Questions

Possible Duplicate: When to use “strictfp” keyword in java? What is the use of
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
Possible Duplicate: Java isEmpty() undefined for String? I copied my code from one java
Possible Duplicate: Java - when to use 'this' keyword Is it generally good convention
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Possible Duplicate: Why use document.write? Considering the negative effects of document.write(), why are most
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: When to Use Static Classes in C# Questions in the title..........i would
Possible Duplicate: How to use enable_if to enable member functions based on template parameter
Possible Duplicate: When to Use Double or Single Quotes in JavaScript Are there differences

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.