CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2009
    Posts
    65

    Reference Variable Query

    Consider the following code:

    Code:
    const int con = 100;
    
    int &ref1 = 10;   //Where is this 10 stored?
    int &ref2 = con;
    
    ref1 = 20;
    ref2 = 90;
    
    cout<<ref1<<con<<ref2;
    This outputs: 20 100 90

    My only queries are:

    a) Where is the hard coded number 10 stored?
    b) When ref1 is modified which memory was modified? (10's or ref1 has it's own memory)
    c) Why was const variable not modified but somehow it's reference was?

    Regards,
    Nisheeth Barthwal

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reference Variable Query

    Ummm...that shouldn't compile.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Reference Variable Query

    It doesn't.

  4. #4
    Join Date
    Jun 2009
    Posts
    65

    Re: Reference Variable Query

    nvm, this is not a standard code just found out.
    int const &ref = 10; //legal
    so
    ref = 3; //illegal
    turns out fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured