Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Beginner C++ question on 'const' keyword.?

Just wondering when and where 'const' should be used.

Are there downsides to using const where it isn't exactly needed? For example, does it somehow take up more memory off the heap/stack?

Also a quick second question.

My book would give function headers that look something like:

someType function(param1, param2, ...) const { .....

return someTypevar;

}

Would the 'const' in this case mean that the object returned by this method/function be unchangeable?

My teacher also told me that when overloading operator methods such as +=, -=, *=, or /=, that we should leave the const out while we should leave const in the header when the operators are +,-,*,/. Why in this case should const be left in.

Sorry if this seems like a beginner question. Coming into C++ from a background in Java and 'const' is new to me.

2 Answers

Relevance
  • 3 years ago

    const is used to create constants that the compiler will not let you modify, but they are mostly used to assure programmers that their variable will not be changed.

    For example:

    1)

    const int foo(int x);

    this will return a constant value that you will not be able to modify (you can see how this is more of a hindrance)

    2)

    char * strcpy ( char * destination, const char * source );

    as you can see the second argument is const, however that doesn't mean the compiler will not let the programmer who wrote that function modify the string passed. What he can't modify is the pointer upon receiving, but the string it points to is fair game for the compiler.

    in this example you can see how const is actually used to inform the user that his string is safe and will not be modified (in contrast to the first argument, that will clearly be changed)

    3)

    class MyClass{

    int a;

    void foo(int b) const {return a+b;}

    void bar(int b) {return a+b};

    }

    for our third example, we have const right before the body of the function. This is to inform the compiler that the body of the function will not modify any of the fields of the class it belongs to.

    for example, if i have

    const MyClass x = MyClass();

    the only way i can call any methods on x is to inform the compiler that the fields of x will not be modified. For this example, calling bar() on x is right out of the question.

    apart from uses in classes, i don't know what else this type of const is good for

  • 3 years ago

    The purpose of "CONST" is to create a variable that never changes.

    For instance, you could use PI and set it equal to 3.14 ... (up to 10 digits, for instance)

    Now that you have a constant named PI, you can use PI whenever you want to use the value 3.14...

    And with a CONST, the program can not accidentally change the value.

Still have questions? Get your answers by asking now.