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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:41:06+00:00 2026-05-25T14:41:06+00:00

I started to read a book about C++ and found the following code. It

  • 0

I started to read a book about C++ and found the following code.
It is an example on how you can send pass parameters by reference.

#include <iostream>

void swap(int &x, int &y);

int main()
{
    int x = 5, y = 10;

    std::cout << "Main. Before swap, x: " << x
              << " y: " << y << "\n";
    swap(x, y);
    std::cout << "Main. After swap, x: " << x
              << " y: " << y << "\n";
    return 0;
}

void swap(int &rx, int &ry)
{
    int temp;

    std::cout << "Swap. Before swap, rx: " << rx
              << " ry: " << ry << "\n";

    temp = rx;
    rx = ry;
    ry = temp;

    std::cout << "Swap. After swap, rx: " << rx
              << " ry: " << ry << "\n";
}

.

Main. Before swap, x:5 y: 10
Swap. Before swap, rx:5 ry:10
Swap. After swap, rx:10 ry:5
Main. After swap, x:10, y:5

The logic is clear to me.
Now this may be a very stupid question (I’m not very experienced yet), but why can’t you just declare private: int x as an instance variable? Isn’t x in this case directly accessible everywhere in your class? (without the need for specifying parameters at all)? Thanks in advance for your answers!

  • 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-25T14:41:07+00:00Added an answer on May 25, 2026 at 2:41 pm

    For several reasons.

    1. You should declare variables at the narrowest scope possible. Why? Look at 2 & 3
    2. Variables are expensive, they take up memory, you only want them
      around as long as you need them.
    3. The greater a variables scope (i.e. how much code the variable is visible
      to) the greater the chance that you will mistakenly use the
      variable, and therefore it’s value may change unexpectedly. This will be a bug, good luck hunting that one down.
    4. Tight coupling (this is bad). If you write a class and put a swap method on the
      class, and you write it so it uses instance member x (not method
      variable x), then that swap method CAN ONLY EVER swap using x, if
      in time you need it to swap on a different variable (or the
      parameter of another method on the class) then you’ve to move the
      value into x which is Inefficient & goto 5. Isn’t it better to call the swap function with the values you have to hand, without needing to know there’s a special x variable that you have to set first?
    5. Error prone. Will this second method be called while another method
      is using the swap method? What should the value of x be after it’s
      called? You’re introducing lots of context around swap and knowing when it’s
      ok to call swap, and what can call swap. This is bad, the more self contained any piece of code is, then the less of have to worry about it, and about how it’s used.
    6. No other class can re-use your swap method, every class that needs a
      swap method must implement it’s own, and this is a huge big
      no-no
      for more reasons than I can count here, but can sum up as it
      voliates the DRY Principal

    All of these problems can be removed by simply passing the values by reference. Bit of a no-brainer really 🙂

    Hope this helps.

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

Sidebar

Related Questions

I just started to read about this new technology... Does someone have some knowledge
When I first started reading about and learning ruby, I read something about the
I've just started using subversion, and have read the official documentation (svn book), cheat
I started to read the book numerical recipes in C... I try make my
I'm new to Android app programming and started to read a book, but it
i started to read Entity Framework Book. in first part of book it describes
I just started with Python 3. In a Python book i read that the
I started to read several tutorials about RequireJS. In none of them was the
I have started learning VB.NET lately and have read 1 Step-By-Step beginners book and
We started following the CakePHP Blog tutorial hosted on the website cakephp.org - http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

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.