#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4

    Reference to constant in C++


    Code:
    const int &val = 10;
    this statement is working in cpp.
    but according to rules of reference we cant have reference to constant value.

    then how is it possible?
    can anybody explain me??
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0
    It creates a temporary variable equal to 10, and stores a reference to that. Const references can extend the life of a temporary to until the reference goes out of scope.

    Comments on this post

    • eramit2010 agrees
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by King Mir
    It creates a temporary variable equal to 10, and stores a reference to that. Const references can extend the life of a temporary to until the reference goes out of scope.
    Thanks for info dude.

IMN logo majestic logo threadwatch logo seochat tools logo