tags:

views:

60

answers:

5
#include <iostream>

using namespace std;

class Imp
{
    public:
        int X(int) {return 50;}
        int Y(int y) {return y;}
};

int main()
{
    Imp i;
    cout << i.X(100) << endl;

    return 0;
}

This code works and prints out 50. My question is what happens to the argument passed? Just out of curiosity. :)

+1  A: 

The argument is passed, and ignored.

Your code does not use it.

Didier Trosset
what do you mean by ignored?
Chubsdad
I simply mean that your code does not uses it.
Didier Trosset
A: 

The exactly same thing happens, you just didn't name the parameter and therefore can't access it.

Let_Me_Be
+2  A: 

The arguments for such parameters are passed in the usual way, but it is not possible to acess such parameters by name in the corresponding function definition (unusual machine architecture specific code can be written however to access the argument)

A good example is the postfix operator++ (and --) as well which is usually overloaded as

struct A{
   A operator++(int){ 
       // stuff
       // return an appropriate A object
   }
};

NB: The compiler usually passes a dummy argument 0 for such overloads

$13.5.7/1- "When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero.136"

The dummy parameter type is indicated as 'int' and is not named almost always(and hence not used in the operator definition)

Chubsdad
Why do u need an int param for operator++? I thought `A a = 1; a++;` is enough.
nakiya
Chubsdad
mmmm. Didn't know that :D.
nakiya
A: 

Somtimes you need a specific signature although the parameter is not used inside the function. In this case you can omit the parameter name to avoid a compiler warning.

Henrik
A: 

Firing up the compiled program in gdb you can see that at assembly level the argument is being passed, but ignored inside the called method:

Dump of assembler code for function main:
0x0000000100000ce3 <main+0>:    push   %rbp
0x0000000100000ce4 <main+1>:    mov    %rsp,%rbp
0x0000000100000ce7 <main+4>:    sub    $0x10,%rsp
0x0000000100000ceb <main+8>:    lea    -0x1(%rbp),%rdi
0x0000000100000cef <main+12>:   mov    $0x64,%esi ; ** 0x64 = 100 = your argument
0x0000000100000cf4 <main+17>:   callq  0x100000d82 <dyld_stub__ZN3Imp1XEi>

And then inside the X method the value 0x32 (50) is returned ignoring parameter passed from the main method.

Dump of assembler code for function _ZN3Imp1XEi:  ; X();
0x0000000100000d1e <_ZN3Imp1XEi+0>: push   %rbp
0x0000000100000d1f <_ZN3Imp1XEi+1>: mov    %rsp,%rbp
0x0000000100000d22 <_ZN3Imp1XEi+4>: mov    %rdi,-0x8(%rbp)
0x0000000100000d26 <_ZN3Imp1XEi+8>: mov    %esi,-0xc(%rbp)
0x0000000100000d29 <_ZN3Imp1XEi+11>:   mov    $0x32,%eax ; ** 0x32 = 50 = your return
0x0000000100000d2e <_ZN3Imp1XEi+16>:   leaveq 
0x0000000100000d2f <_ZN3Imp1XEi+17>:   retq   

Hope it was helpful or maybe at least interesting to have a look at. Even when you are not fit in assembler it can sometimes help to dig in and look around

gilligan