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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:06:16+00:00 2026-06-17T19:06:16+00:00

The following code prints: 2 1 instead of 2 2 Why is my setter

  • 0

The following code prints:

2
1

instead of

2
2

Why is my setter not adjusting the value?

Main

Vector location = camera.get_location();
camera.get_location().set_y(location.get_y() + 1);
std::cout << location.get_y() + 1 << std::endl;
std::cout << camera.get_location().get_y() << std::endl;

camera.h

#ifndef CAMERA_H
#define CAMERA_H

#include "vector.h"

class Camera {
 private:
  Vector location;
 public:
  Vector get_location();
  void set_location(Vector);
};

#endif

camera.cpp

#include "camera.h"

Vector Camera::get_location() { return location; }
void Camera::set_location(Vector l) { location = l; }
  • 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-17T19:06:17+00:00Added an answer on June 17, 2026 at 7:06 pm
    camera.get_location().set_y(location.get_y() + 1);
    

    get_location returns a copy of the original object. So set_y does modify y but it is modiying the copy of the original location. If you want the above to work as you expect, then return reference:

    Vector & get_location();
    

    The function-body will be same as before:

    Vector& Camera::get_location() { return location; }
    

    Now it will work the way you expected.

    You could write the code as:

    Vector  & location = camera.get_location(); //save the reference
    location.set_y(location.get_y() + 1);
    

    It modifies the camera‘s location object.

    Compare the above code with this:

    Vector location = camera.get_location(); //save the copy!
    location.set_y(location.get_y() + 1);
    

    It does NOT modify camera‘s location object! It modifies the copy, not the original.

    Hope that helps.

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

Sidebar

Related Questions

For example the following code prints { key: 'b' } function myFunc(key, value) {
the most darndest thing! the following code prints out 'llo' instead of the expected
The following code prints the highest busy value in every 5 minute increment. How
The following code prints out 10 . How can I make it print out
The following code prints (when invoking MyMethod): 0 0 0 1 I would expect
The following code prints null once. class MyClass { private static MyClass myClass =
Why is the following code prints xxY? Shouldn't local variables live in the scope
The following java code prints Hello World on my co-worker's computer: public void testJDBC()
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
The following code with VS2010 prints 0 , contrary to my expectations: #include <complex>

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.