const confusion

I have 2 questions regarding the following code

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!


Thanks!

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
35
36
37
38
39
40
41
  #include <iostream>

class counter {
  public:
  int i;

  counter();

  int inspect() const;
  void increment();
};

counter sigma_inspect;

counter::counter()
{
  i = 0;
}

int counter::inspect() const
{
  sigma_inspect.increment();
  return i;
}

void counter::increment()
{
  ++ i;
  return;
}

int main(void)
{
  counter a;

  std::cout << a.inspect() << "\n";

  std::cout << sigma_inspect.inspect() << "\n";
  std::cout << sigma_inspect.inspect() << "\n";
  return 0;
}
As the compiler said:
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.
Last edited on
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).

Aceix.
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 const object you'll need a const method!

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 .

Is my above statement correct ??
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.

Aceix.
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. static members and methods
3. const members 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:

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
35
36
37
38
#include <iostream>
using namespace 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; }

	static int static_get() { std::cout << "I'm static" << endl; return value2; }
private:
	int value = 0;
	static int value2;
	volatile int value3 = 5; // can be changed outside the process
	volatile static int value4; // the same but no object instance required
};

int MyClass::value2 = 0;
volatile int 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
volatile is very seldom used. AFAIK the only time it's useful is for dealing with direct memory-mapped IO writes (like for driver development)
Registered users can post here. Sign in or register to post.