benefits of const reference ?

This is a discussion on benefits of const reference ? within the C++ Programming forums, part of the General Programming Boards category; hi all~ i read this in a book which showed me an example with const reference in template: Code: template ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    benefits of const reference ?

    hi all~

    i read this in a book which showed me an example with const reference in template:
    Code:
    template <class T>
    T Abc(const T& a, const T& b, const T& c)
    {
        return a + b + b * c + (a + b - c) / (a + b) + 4;
    }
    it didnt mention the advantages using const reference here, any ideas ?

    thanx~
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    if T is an int there is no advantage, if it's a short or char a less agressive compiler might even be a bit slower, but if T was a 2000 X 2000 matrix then the reference saves you three very big copy operations. const references can also be bound to temporaries, thus,

    Code:
    void center(const std::string &s, int col=80) {
        int padding = (col - s.length() )/2;
        for(int i=0;i<padding;++i) std::cout << ' ';
        std::cout << s;
    }
    
    std::string hi="hello";
    center(hi); // no copy of hi made
    center(std::string("A String")); // temporary created, used, discarded
    center("this also works"); // the compiler will create a temp here automatically.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx i know reference can run faster, but what really confused me is the meaning of "const reference", and what is the difference between reference and const reference here ?

    thanx~
    Never end on learning~

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm guessing a const reference stops you from changing the value of the variable. Ie:
    Code:
    int a=5;
    int &b=a;
    
    b=7; //not allowed
    However I may be wrong.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by bennyandthejets
    I'm guessing a const reference stops you from changing the value of the variable. Ie:
    Code:
    int a=5;
    int &b=a;
    
    b=7; //not allowed
    However I may be wrong.
    thanx bennyandthejets

    if follows you then these two will be same because parameters not changed:
    Code:
    template <class T>
    T Abc(const T& a, const T& b, const T& c)
    {
        return a + b + b * c + (a + b - c) / (a + b) + 4;
    }
    template <class T>
    T Abc(T& a, T& b, T& c)
    {
        return a + b + b * c + (a + b - c) / (a + b) + 4;
    }
    and i think there may be other usage for const reference, perhaps it can help for better performance than simple reference ?
    Never end on learning~

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    In general 'const' does not affect performance. Rather, it enforces good programming techniques.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    bennyandthejets are right and a const reference can only invoke const member-functions. As for your two code-snippet comparisment you are right. The code doesnīt differ (except for the const-ness of course), but the compiler cannīt analyse (yet, and I think itīs part of the standard) the code and know if the object state is changing. In this case anyone can see that the const are unneccesary (but as bennyandthejets pointed out "it enforces good programming techniques", better learn it now) but as code can get very complex this might not be as easy. And because compiler are consistent the rule aplies for non-complex and complex code. Have you ever considered why an array have to have a constant expression?

    Code:
    ...
    int arraySize = 5;
    int array[arraySize];
    ...
    Why does it generate a compiler error? Well it demostrates the same "problem" you encountered. How should the compiler know if you deside to change value on arraySize?
    Code:
    ...
    if (flagon)
    arraySize = 10;
    ...
    int array[arraySize];
    Only through heavy code-analysis and if the code is very complex this might not be as easy. Consistent is the key word here.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  3. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 04:13 PM
  4. const reference formal arguments
    By Chaplin27 in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2004, 11:12 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 12:02 PM

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