Reference with constant modifier
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Reference with constant modifier

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    17

    Reference with constant modifier

    double &val = 66.6; //illegal
    const double &val = 66.6; //legal
    I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
    can anyone please let me know where exactly we can use this concept in real time programming ?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,505

    Re: Reference with constant modifier

    In real time programming we don't implement any concepts, just make things done... Mostly in C.

  3. #3
    Join Date
    Jan 2012
    Posts
    17

    Re: Reference with constant modifier

    int nVar = 12;
    int &rVar = nVar ;//Ok
    double &dVar = nVar ;//Error
    const double &cdVar = nVar ;//Ok

    can you please explain Why the 3rd statement is not working where as 4th statement is working ?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Reference with constant modifier

    Quote Originally Posted by vikuseth View Post
    int nVar = 12;
    int &rVar = nVar ;//Ok
    double &dVar = nVar ;//Error
    const double &cdVar = nVar ;//Ok

    can you please explain Why the 3rd statement is not working where as 4th statement is working ?
    What is "dVar"? What is "cdVar"?

    Please post real code that can be compiled, and please use code tags. If you're asking a question why something doesn't compile, it is imperative you post the actual, real bonafide C++ code that doesn't compile, and not shortcuts or psuedo-code.

    Edit: Look at my post and look at yours. Do you see why you need code tags? No one knows whether that & is a reference, or the fault of your posting.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Reference with constant modifier

    Quote Originally Posted by vikuseth View Post
    double &val = 66.6; //illegal
    const double &val = 66.6; //legal
    I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
    A const reference can bind to an r-value, a (non-const) reference cannot. The literal is an r-value. That's all the magic.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center