Const in method definitions

Mar 29, 2011 at 5:42pm
I'm kind of confused with "consts" in this example. How can I change *this in the method (for example operator++) when there is "const" placed after method declaration -
const Complex Complex::operator++() const

I can do whatever I want to, just put *real = 123 - and it works....
But how???, when there is a const after method declaration.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;

class Complex
{
private:
    double *real; 
    double *imag; 
public:
	Complex() {
	real = new double  ;
		*real = 0; 
    imag = new double  ;
		*imag = 0; 
	}
	Complex(double realnum, double imagnum){
		real = new double  ;
		*real = realnum; 
		imag = new double  ;
		*imag = imagnum; 		
		}

	double GetReal(void)const{return *real;};
	double GetImag(void)const{return *imag;};	
	void SetReal(double r){*real=r;}
	void SetImag(double i){*imag=i;}

        const   Complex operator++() const; 
        const   Complex operator++(int) const ;
};


    const Complex Complex::operator++(int i) const                   
    {
             *real = *(real) + 1;           
             return *this;
            
    }

      ostream &operator << (ostream &os, const Complex &num)	
    {
		
              os << "(" << num.GetReal() << "," 
                 << num.GetImag() << "i)";
	      return os;
     }

      const Complex Complex::operator++() const
    {
        Complex comp(*(real), *(imag));    
        *(real) = *(real) + 1;      
        return comp;   
    }
int main()
{
    Complex a(3.0, 4.0);   // initialize to (3,4i)
     cout << " ++a  is "<<++a<<endl;
     cout << " a++  is "<<a++<<endl;
    getchar();
	return 0;
}


   ++a is(3,4)
   a++ is(5,4)





Last edited on Mar 29, 2011 at 8:28pm
Mar 29, 2011 at 6:00pm
a const method doesn't allow you to modify the value of the members, but *real = *(real) + 1; does not modify real, it modifies the object real is pointing to.

eg:

1
2
3
4
5
6
7
8
9
10
11
struct S
{
   int a, *p;
   void f() const
   {
        a = 0; // Error: modifies 'a'
        p = &a; // Error: modifies the value of 'p'
        *p = 0; // OK: it doesn't modify the value of 'p' itself but the value of the integer pointed by 'p'
   }
};
Last edited on Mar 29, 2011 at 6:21pm
Mar 29, 2011 at 6:07pm
I believe that that const functions are functions that can be invoked on objects declared as const.
It doesn't stop you from changing the state of the object in the function.

For example:
1
2
3
const Complex c;
c++; // this will not throw a compile-time error in the above code,
     //   even though it doesn't make sense to change a const object's state! 


FYI: generally, the prefix operator++(void) adds then returns, and the postfix operator++(int) adds but returns the old value.
Mar 29, 2011 at 8:09pm
Thanks guys for your help.
Finally I got it.

I cannot change the value of my pointer real, if there is "const" after definition.
My mistake was that I was trying to change *real thinking that I'm changing real (the pointer),
but it is no true, I was just changing the value of the object that it was pointed to.
But in order to change my pointer real I should have done it like this:

1
2
 double p; 
 real = &p;


In this case I cannot do it, because of "const" and because now I'm really changing my
pointer:
1
2
3
4
5
6
7
8
9
10
11
12
  Complex Complex::operator++(int i)  const          
    {      
     
      Complex comp(*(real), *(imag));  
      *(real) = *(real) + 1;      
         
             
      double p;             //mistake 
      real = &p;            //mistake   

      return comp;     
    }
Mar 29, 2011 at 8:14pm
Also, could anybody please explain the meaning of the "const" before method definition?

What dose this "const" mean which is before Complex?

1
2
3
4
5
  const Complex Complex::operator++() const
    {
        *real = *(real) + 1;           
         return *this;
    }

Mar 29, 2011 at 8:55pm
It means that the function returns a constant Complex object

const Complex Complex::operator++() const
{
*real = *(real) + 1;
return *this;
}



EDIT:
I believe the constant is redundant in this case (as the function is returning a copy, which is treated
as constant anyway)
Last edited on Mar 29, 2011 at 9:14pm
Mar 29, 2011 at 9:34pm
I understand it, but could you show more specific example of using
"const" before method definition. I cannot understand why you need to use
it at all....
Mar 29, 2011 at 10:03pm
The constant might be at the front of the function definition/declaration - but as I say it is related to the object being returned by the function and has not effect on the function itself.
Topic archived. No new replies allowed.