Value semantics vs reference semantics

Value Semantics:

Behavior where variables are copied
when assigned to each other or passed as parameters.

  • Primitive types in C++ use value semantics.
  •  Modifying the value of one variable does not affect other   Example:
    int x = 15;
    int y = x; // x = 15, y = 15
    y = 17; // x = 15, y = 17
    x = 18; // x = 18, y = 17

Reference Semantics: Behavior where variables refer to a
common value when assigned to each other or passed as

  • Object types in C++ use reference semantics.
  •  Object variables do not store an object; they store the address of
    an object’s location in the computer memory and graphically
    addresses are represented as arrows.

Point p = new Point(30 ,28);

If two object variables are assigned the same object, the
object is NOT copied; instead, the object’s address is

  • As a result, both variables will point to the same object.
  • Calling a method on either variable will modify the same object.

Point x = new Point(13,12);
Point y = x;


Why Reference Semantics ?

Objects have reference semantics for following reasons:

  • efficiency: Objects can be large and bulky. Having to copy them
    every time they are passed as parameters would slow down the
  • sharing: Since objects hold important state, it is often more
    desirable for them to be shared by parts of the program when
    they’re passed as parameters. Often we want the changes to
    occur to the same object.

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.