const after function name

Oct 14, 2017 at 5:47pm
What does const after the function name Primeter mean here in the below program ? I am not attaching the full code as it was somewhat big. I just want to understand meaning of const after function name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

class Sticker
{
private:
double Perimeter() const;
double Area() const;
double Height:
public:
void GetDimensions();
void Properties();

}
Last edited on Oct 14, 2017 at 5:48pm
Oct 14, 2017 at 6:08pm
Two parts.

1. Whoever will use Sticker objects knows that those functions will not change the object and can be called on const objects.

(example ignores the public/private)
1
2
3
4
5
6
7
Sticker foo;
foo.Area(); // ok
foo.Properties(); // ok

const Sticker bar;
bar.Area(); // ok
bar.Properties(); // error, bar is const 


2. When you implement double Sticker::Area() const the compiler will check that you don't attempt to modify the object within the object.

3. One can overload, have both const and non-const version of member function. For example:
http://www.cplusplus.com/reference/vector/vector/at/
Oct 15, 2017 at 12:56am
I have still not understood. Please can you explain in a simple language through an example that what will happen if we do not use const ?
Last edited on Oct 16, 2017 at 7:35am
Oct 16, 2017 at 10:47am
Please can you explain in a simple language through an example that what will happen if we do not use const ?


1) The compiler will not check that the method Sticker::Area()[b][/b] leaves the Sticker object unchanged. If you modify that method definition, and accidentally change it so that it changes the state of the object, then the compiler will not throw an error.

2) You will not be able to call the Area method on a non-const Sticker object - as in line 7 of keskiverto's example.
Oct 16, 2017 at 2:08pm
through an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

class MyClass {
    int myprop {};
public:
     // A const method can't modify class properties:
    // int modMyprop1() const { return ++myprop; } // error: increment of member 
                                                   // 'MyClass::myprop' in 
                                                   // read-only object

    int modMyprop2() const { return myprop + 1; } // fine - myprop is untouched

    int modMyprop3()       { return ++myprop; } // this is not const, so it's
                                                // allowed to modify myprop;
                                                // on the other hand, it can't 
                                                // be invoked by instances
                                                // of MyClass which are 
                                                // declared 'const'
};

int main()
{
    MyClass myinst1; // not const
    // myinst1 can invoke every method (const and not const)
    std::cout << "myinst1.modMyprop2(): " << myinst1.modMyprop2() << '\n';
    std::cout << "myinst1.modMyprop3(): " << myinst1.modMyprop3() << '\n';

    const MyClass myinst2;
    // myinst2 can invoke only const methods
    std::cout << "myinst2.modMyprop2(): " << myinst2.modMyprop2() << '\n';
    // error: passing 'const MyClass' as 'this' argument discards qualifiers:
    // std::cout << "myinst2.modMyprop3(): " << myinst2.modMyprop3() << '\n';
    return 0;
}

Topic archived. No new replies allowed.