How does this work?

In this Code:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
class Digit
{
private:
    int m_nDigit;
public:
    Digit(int nDigit=0)
    {
        m_nDigit = nDigit;
    }
 
    Digit& operator++(); // prefix
    Digit& operator--(); // prefix
 
    Digit operator++(int); // postfix
    Digit operator--(int); // postfix
 
    int GetDigit() const { return m_nDigit; }
};
 
Digit& Digit::operator++()
{
    // If our number is already at 9, wrap around to 0
    if (m_nDigit == 9)
        m_nDigit = 0;
    // otherwise just increment to next number
    else
        ++m_nDigit;
 
    return *this;
}
 
Digit& Digit::operator--()
{
    // If our number is already at 0, wrap around to 9
    if (m_nDigit == 0)
        m_nDigit = 9;
    // otherwise just decrement to next number
    else
        --m_nDigit;
 
    return *this;
}
 
Digit Digit::operator++(int)
{
    // Create a temporary variable with our current digit
    Digit cResult(m_nDigit);
 
    // Use prefix operator to increment this digit
    ++(*this);             // apply operator
 
    // return temporary result
    return cResult;       // return saved state
}
 
Digit Digit::operator--(int)
{
    // Create a temporary variable with our current digit
    Digit cResult(m_nDigit);
 
    // Use prefix operator to increment this digit
    --(*this);             // apply operator
 
    // return temporary result
    return cResult;       // return saved state
}
 
int main()
{
    Digit cDigit(5);
    ++cDigit; // calls Digit::operator++();
    cDigit++; // calls Digit::operator++(int);
 
    return 0;
}


How can the compiler distinguish the prefix and postfix increment and decrement operators? I understand the dummy variable but

cDigit++; // calls Digit::operator++(int);

How does that call Digit::operator++(int);?

You didn't use the dummy variable a parameter?
@Anmol444
You didn't use the dummy variable a parameter?


But you use either prefix or postfix operator. According to the used operator the compiler substitutes it for the corresponding function call.
Last edited on
I know you are using either but how does that call that if you dont use it? And what do you mean substitutes it for the corresponding function call?
When the compiler sees for example the following statements

Digit d( 7 );

++d;

it substitute ++d; for

Digit::operator ++();

If the compiler sees

d++;

it substitute it for

Digit::operator ++( 0 );
Last edited on
But why??? We didn't give it a int parameter so how?
Or is it just like that?
What is "why"?! Do you see the difference between ++d and d++?! And the compiler sees it and substitute it for a call of the corresponding function.
Last edited on
$ dict dummy
Silent; mute; noiseless
Fictitious or sham; feigned
having the appearance of being real but lacking capacity to function
I do except the thing is d++ does not provide a integer argument... So how does the compiler know that you want that?
Last edited on
The second time I am asking you: do you see the difference between two records ++d and d++?
Yes I do.
Oh thanks moscow!

You rule! :D
@Anmol444

I do except the thing is d++ does not provide a integer argument... So how does the compiler know that you want that?


The argument is not used. It serves only to the operator overload. So there is the relation

++d <===> d.operator ++()
d++ <===> d.operator ++( 0 )

So when the compiler sees ++d it substitutes it for d.operator ++(). When the compiler sees d++ it substitutes it for d.operator ++( 0 ). The argument is not used so in fact the compiler can use any value for the argument. So there is no problem.

When a user wants explicitly to specify the functional call of operator he need also to specify some argument for the postfix operator.

d.operator ++( 0 );

Again the argument is not used in the body of the operator-function. It is used to distinguis this operator from the prefix operator that looks as

d.operator ++();
Last edited on
ok thanks :D
Topic archived. No new replies allowed.