what does this line of code mean??

I think this is a function returning a bool,

 
  bool atEnd() const;


but what does the
"const" before the";" mean ?
Last edited on
You have omitted essential context.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Foo {
public:
  bool foo() const { return true; }
  bool bar() { return true; }
};

int main() {
  Foo hello;
  const Foo dolly;

  bool a = hello.foo(); // OK
  bool b = hello.bar(); // OK

  bool c = dolly.foo(); // OK
  bool d = dolly.bar(); // Error. dolly is const.  bar might change dolly.

  return 0;
}


That const ensures that the member function will not change the object.
const here means that the function will not modify any of the member variables. When you have a const object (or a pointer/reference to const) you can only call member functions that are marked with const.

1
2
3
4
5
6
7
const std::string str = "Hello";

std::cout << str.length() << std::endl;
// OK, std::string::length() is const

str.resize(2); 
// ERROR, std::string::resize() is not const 

You have omitted essential context.
It's a line from QString.h and I haven't localized QString.cpp yet i'm beginnening to understand

Ok thanx ;)

http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Basically ‘const’ applies to whatever is on its immediate left
Last edited on
Topic archived. No new replies allowed.