Thread: 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,149
    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
    28,413
    Read Stroustrup's answer to: Should I put "const" before or after the type?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,815
    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, 08: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-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM