2

I can run the following code without problem. I notice both a.bytes and bytes in main changed to "B". So what does the "const" do in A's get() function? Is it going to be a problem in my usage of changing the bytes I get from A as below?

Note: I don't care if A's value will change. I just want to know if I will encounter unpredictable problems, especially when I free A at its deconstructor no matter whatever crazy action I take with the bytes in main.

class A{
public:
    A(){
        bytes = (char *)malloc(12);
        bytes[0] = 'A';
        bytes[1] = 0;
    }
    ~A(){
        free(bytes);
    }
    char * get() const{
        return bytes;
    }
    char * bytes;
};

int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    char * bytes = a.get();
    bytes[0] = 'B';

    return 0;
}
K--
  • 659
  • 1
  • 7
  • 18
  • 4
    Is there a reason you're using `malloc` and `free` in a C++ program? – Some programmer dude Oct 15 '14 at 10:58
  • 1
    And please don't use c-style casts in C++, `bytes = (char *)malloc(12);` is just bad – UnholySheep Oct 15 '14 at 11:02
  • @UnholySheep, in what situation would c-style cast of pointer returned by `malloc` have unexpected results? – eerorika Oct 15 '14 at 11:03
  • It's just a simplified version of cocos2d-x's Data class (ver 3.x) – K-- Oct 15 '14 at 11:07
  • 2
    @user2079303 while it may not necessarily have unexpected results there are certain reasons why you should not do it. These have been discussed often enough on various sites (including SO). Even so, you should not use C-style casts in C++, as this is a bad habit that can easily lead to errors and unexpected behaviour – UnholySheep Oct 15 '14 at 11:14
  • The first two "Related" links on the right of the page: http://stackoverflow.com/q/751681/1918193 and http://stackoverflow.com/q/1143262/1918193 perfectly answer this question... – Marc Glisse Oct 15 '14 at 11:46
  • possible duplicate of [What is meant with "const" at end of function declaration?](http://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration) – Niall Oct 17 '14 at 08:01
  • In addition to the method being `const` and thus not allowing it to modify the object contents (`*this`), it should return a pointer that is also `const` thus not immediately allowing the internals to be altered either (as does in the sample). Hence favour a signature such as `char const * get() const`. Const-correctness involves more that just marking a method as `const`. – Niall Oct 17 '14 at 08:07

4 Answers4

4

It simply means that the member function will not (and can't) modify any internal data inside the object.

It also causes the this variable inside the member function to be const.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I see. Is there any chance that it causes problem when ~A() is called? – K-- Oct 15 '14 at 11:02
  • I don't agree with you. You can want a const pointer pointing on a non-const value. But, here it's clearly not the case. – Caduchon Oct 15 '14 at 11:02
  • 1
    @JoachimPileborg I disagree on the undefined behavior. `const` can always be legally cast away unless the original object is declared `const`. Of course, this would be horrible programming practice but a compiler cannot optimize on this. – pmr Oct 15 '14 at 11:26
  • As making this pointer const, Does it prevent the returning value out of scope? – Cahit Burak Küçüksütcü Oct 15 '14 at 15:09
3

It means that the function doesn't modify the members of the class (only members with the key-word mutable can be modified). It also means that the function doesn't call other non-const functions of the class.

Caduchon
  • 4,574
  • 4
  • 26
  • 67
1

To add a note, when const put after a member function it means that the "this" pointer passed to the member function will be const, Hence only mutable and or read only operations are permitted.

Danny Shemesh
  • 67
  • 1
  • 9
0

It means you can only modify mutable member variables of the object. This is useful if you have a const object, but still want to use some non-mutating member functions.

OMGtechy
  • 7,935
  • 8
  • 48
  • 83