1-> why can't i call the increment function inside the inspect function by
writing : counter::increment() instead of using the object sigma_inspect.incremen() (line 22) ?
2->const functions dont change the state of the object , but here , why is object a changing the state of sigma_inspect making the output come out to be
0
2
3
or is my understanding of "const functions " not right ? #getting confused!
In member function 'int counter::inspect() const': 22:13: error: passing 'const counter' as 'this' argument of 'void counter::increment()' discards qualifiers.
If you just write increment() in counter::inspect(), you are essentially writing this->increment(). Here "this" is a pointer pointing to the object you are using.
For example, when you write a.inspect(), the "this" pointer is pointing to the object a. Because inspect() has a const qualifier, the "this" pointer is a pointer of const counter, which means you cannot change the state of the object a via "this", nor can you call another member function with a non-const qualifier.
For your second question, because you are directly calling increment() on sigma_inspect (without using the "this" pointer), the function's promise of not using "this" pointer to change the state of the object it is referring to still holds.
You are not able to call increment() that way because it has not been declared static. Static member functions have the ability to be called from outside the class without creating an instance of it.
For the cosnt inspect() function, it will not be able to change the value of a member variable(field) of its own instance. From your code, a's inspect call, tends to call a sigma_inspect's increment function, which is non-const, and so can change the value of I in sigma_inspect. What I'm saying is, a const member function cannot change a field with the this pointer(ie: under its own instantiation).
or is my understanding of "const functions " not right ? #getting confused!
the following code will not work becase there is no const method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class MyClass
{
public:
int get() { return value; }
private:
int value = 0;
};
int main()
{
// constant object
const MyClass instance;
/// this will not work!
int x = instance.get();
return 0;
}
in order to call the method on constobject you'll need a constmethod!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class MyClass
{
public:
int get() { return value; }
int get() const { return value; } // this one will enable the call
private:
int value = 0;
};
int main()
{
// constant object
const MyClass instance;
/// now works!
int x = instance.get();
return 0;
}
The member of a class can access the protected and public member of the class ,
this statement goes for only "data members" like int i in the above case,and not the functions that are listed in the class above.To access the functions you need a object or declare the function to be static .
Member variables(properties) can also be made static. Members of a class can access every part of the class, including private. Inheriting classes can access(inherit) only public and protected, friends too can access all members, and the public is for the outsiders.
vhx,
your statement is somewhat corrent, answering your question in detail will require a long post..
for example there are 4 possible ways to "restrict" access to data member and methods:
1. public/ private/ protected
the above modifiers can be furtermore granulated int:
2. staticmembers and methods
3. constmembers and methods
4. volatilemember and methods
this gives us a total of 12 possible "access" combinations
for example the above example for const methods will not work for external processes and will also not work for static data members, to solve this your class may look something like this:
#include <iostream>
usingnamespace std;
class MyClass
{
public:
int get() { cout << "I have no modifier" << endl; return value; }
int get() const { cout << "I'm const" << endl; return value; }
int get() volatile { cout << "I'm volatile" << endl; return value3; }
staticint static_get() { std::cout << "I'm static" << endl; return value2; }
private:
int value = 0;
staticint value2;
volatileint value3 = 5; // can be changed outside the process
volatilestaticint value4; // the same but no object instance required
};
int MyClass::value2 = 0;
volatileint MyClass::value4 = 0;
int main()
{
MyClass instance1;
const MyClass instance2;
int x = instance1.get(); // will call get()
int a = instance2.get(); // will call get() const;
int y = MyClass::static_get();
int z = instance2.static_get();
volatile MyClass instance3;
int b = instance3.get(); // will call get() volatile;
std::cin.ignore();
return 0;
}
copy and paste the code to see the output on your own.
output:
I have no modifier
I'm const
I'm static
I'm static
I'm volatile
Thanks for the awesome example , i wonder why they didn't cover volatile in the cplusplus class tutorial? anyways this example made my mind clear off a ton of things