Questions tagged [const-method]

23 questions
1
vote
0 answers

c++ const member function that accepts modification of specified member variable (relaxed const)

I have the following class Sample. Say, I wanna perform some heavy computation using member variable var and cache the result in case I will do the same computation again. I also guarantee that non-cached variable var be const, and if not, I wanna…
orematasaburo
  • 1,207
  • 10
  • 20
1
vote
2 answers

Might this cause an infinite loop?

For defining a second const version of a function, is it guaranteed safe to do this? It looks like it would have infinite recursion as I want to return const but the other function which I mean to call is non-const. It works with g++ but I worry…
Zhro
  • 2,546
  • 2
  • 29
  • 39
0
votes
0 answers

Error passing reference const to function getting: the object has type qualifiers that are not compatible with the member function

I have this simple function : DBConfig::DBConfig(const IniParser& ini) { m_url = ini.GetValue("host"); m_user = ini.GetValue("db_user"); m_pass = ini.GetValue("db_pass"); m_schemaName = ini.GetValue("db_schema"); } which i…
user63898
  • 29,839
  • 85
  • 272
  • 514
0
votes
1 answer

reinterpret_cast casts away const qualifier?

In the following case I try to cast a pointer to a struct into a pointer to another struct (which has the same memory layout behind the scenes). I try to do this in a const correct way, however the compiler is complaining. I looked at a similar…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

changing a value of a class pointer in const Class function gives an error in some compiler but not others

I have below code and It gives an error as "segmentation fault" in some compiler but for other compilers it gives "40" as output. Why is there difference? #include class A{ int *ptr; public: void set(int val) const; }; void…
Selcuk
  • 109
  • 2
  • 11
0
votes
0 answers

Xcode not recognising const method as const

I have a simple fraction class that I'm working on and I've been overloading various operators and they all work apart from my division operator. For some reason even though it's marked as a const method like all the other operators that return a…
0
votes
1 answer

I can't call constructor of inner class with this pointer of outer class

I don't know why below code failed to compile with error: "no instance of constructor "cb::iterator::iterator" matches the argument list argument types are:(int, const cb)" But the code compiles fine when i uncomment the second version of…
Islam Abdeen
  • 539
  • 3
  • 10
0
votes
5 answers

Why is std::basic_string::operator[] a const method if it's also a non-const method?

http://cplusplus.com/reference/string/basic_string/operator[] I understand that it's advantageous to have a second version which returns const to prevent warnings when a const result is required and to mitigate casting but if the function already…
Zhro
  • 2,546
  • 2
  • 29
  • 39
1
2