-6
#include <iostream>
#include <string>
using namespace std;

template <typename Type>
const Type& GetMax(Type& value1,Type& value2)
{
    if(value1 > value2)
        return value1;
    else
        return value2;
}
template<typename Type>
void DisplayComparison(const Type& value1,const Type& value2)
{
    cout<<"GetMax("<<value1<<","<<value2<<") = ";
    cout<<GetMax(value1,value2)<<endl;
}
int main()
{
    int Int1 = -102, Int2 = 2001;
    DisplayComparison(Int1,Int2);

    double d1 = 3.14,d2 = 3.1416;
    DisplayComparison(d1,d2);

    string Name1("Jack"),Name2("John");
    DisplayComparison(Name1,Name2);

    return 0;
}

const Type& GetMax ... is const necessary? If yes why? and if I write like this--> const Type& GetMax(const Type& value1,const Type& value) what those two const are doing? :(

melpomene
  • 84,125
  • 8
  • 85
  • 148
Simon Maghiar
  • 117
  • 1
  • 1
  • 7
  • 4
    All this and more is explained in any [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep Oct 18 '17 at 14:33
  • That `const` is not qualifying the function itself. It qualifies the return type. Same goes for the potential `const` you mention for that parameters, they would qualify those parameters' type. Member function qualifiers come at the very end, like `void foo() const`. – François Andrieux Oct 18 '17 at 14:36
  • You're over-complicating it, just return `Type` and not `const Type&` – acraig5075 Oct 18 '17 at 14:37
  • @acraig5075 Those two aren't necessarily equivalent. Some types are not copyable and others are expensive to copy. In this case, the function is returning a reference to one of the arguments. Changing the return type would drastically change the function's usage. – François Andrieux Oct 18 '17 at 14:37
  • So If I put a const at the begining of the function especially in this case it means that the return type can't be modified right? – Simon Maghiar Oct 18 '17 at 14:39
  • `const T &` means you can't use that reference to modified the referred object. It doesn't necessarily mean that it can't be modified. It could still potentially be modified using a different non-const reference in another part of your code. – François Andrieux Oct 18 '17 at 14:40
  • Ohh yea I got it ! Thank you @FrançoisAndrieux ! – Simon Maghiar Oct 18 '17 at 14:42
  • also note that returning a non-const reference to a member breaks encapsulation, in that case you could as well make the member public. – 463035818_is_not_an_ai Oct 18 '17 at 14:44
  • If there is no duplicate Q&A, these comments should be potential answers.... –  Oct 18 '17 at 14:47

1 Answers1

0

I am going to put forth a few points about const so that it is clear when and what can you use as required:

Case 1: GetMax(const Type value1)

Here value1 is a local copy, I can think of a couple of points

I can think of a few reasons:

1) When someone reads the code and see const Type value1, they know that value1 should not be modified in the body of the function.

2) The compiler will tell you when you try to modify value1 in the body of the function. Therefore, adding const can prevent mistakes.

3) However, there is another difference in C++11. A constant object cannot be moved from, as a move operation modifies the object. Therefore, you can only make a copy of value1 in the function body and cannot move from it.

4) Also, if this is a class type, you cannot call non-const members functions on a const object.

Case 2: GetMax(const Type& value1)

We don't know how value1 is used outside the function, so we want to protect it from being modified.

Case 3: const Type& GetMax()

Talking about the const before the function, means it will return a const reference to Type (here GetMax)

Take this example:

Class xyz;
Type& t = xyz.GetMax()           //This is INCORRECT
const Type& tc = xyz.GetMax()    //This is OKAY!

So, to sum up, you do not "need to use" const unless you have such requirements as mentioned above, although it might be good practice to do so in a few situations.

Sahil Shah
  • 97
  • 1
  • 6