int const&

This is a discussion on int const& within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, We always write const int other than int const, are they the same? Example, Code: int const & ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    int const&

    Hello everyone,


    We always write const int other than int const, are they the same?

    Example,

    Code:
    	int const & x = 10; 
    
    	const int& y = 10;

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Yes.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Read Stroustrup's answer to: Should I put "const" before or after the type?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    I have read through Bjarne's article as your recommended below. It is a good article.

    But after reading it, I do think think Bjarne has a preference (with reasons) why one form is better than the other form. Agree?

    Quote Originally Posted by laserlight View Post

    regards,
    George

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by George2 View Post
    But after reading it, I do think think Bjarne has a preference (with reasons) why one form is better than the other form. Agree?
    I'd logically have to agree, given that Bjarne opens that section with "I put it before, but that's a matter of taste." and then goes on to explain why he prefers it.

    The basic rule of thumb with const (like volatile) is that it applies to the thing immediately on its left, unless it is on the left in which case it applies to the thing immediately on its right. According to that, your two examples are exactly equivalent.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks grumpy,


    My question is answered.

    Quote Originally Posted by grumpy View Post
    I'd logically have to agree, given that Bjarne opens that section with "I put it before, but that's a matter of taste." and then goes on to explain why he prefers it.

    The basic rule of thumb with const (like volatile) is that it applies to the thing immediately on its left, unless it is on the left in which case it applies to the thing immediately on its right. According to that, your two examples are exactly equivalent.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21