Muralitharan Perumal
Muralitharan Perumal

Reputation: 173

What c++ compiler does when passing arguments to a function in this example?

I am from C background and just trying to understand the c++ compiler behaviour in the following code sample.

 #include <iostream>
 using namespace std;

 class A {
       int _n;
       friend void func(const A& i1, const A& i2 = A());
   public:
     A() : _n(0) {
        cout << "default constructor: _n  " << _n << endl;
     }
     A(int n): _n(n) {
        cout << "constructor: _n  " << _n << endl;
     }
     A(const A& a):_n(a._n) {
        cout << "copy constructor: _n  " << _n << endl;
     }
     A& operator =(const A& a) {
        _n = a._n;
        cout << "assignment operator: -n  " << _n << endl;
        return *this;
     }
     ~A() {}
 };

 void func(const A& i1, const A& i2 ) {
    cout << "value i1._n " << i1._n <<endl;
    cout << "value i2._n " << i2._n <<endl;
 }

 int main( void ) {
    A a(9), b, c;

    func(10);               /* case 1 */
    func(a, A(20));         /* case 2 */
    func(A(c));             /* case 3 */
    b = a;                  /* case 4 */
 }

Declared func as friend of class A to access private data (_n).

case 1: I dont really understand how 10 gets assigned to _n as the value expected in the first argument of func is an object reference. Can someone help me how this is actually working with some example.

case 2: As the compiler evaluates from right to left, a) It calls the constructor (secord argument) and assigns 20 to _n. b) As the object 'a' is already constructed, it just assigns a to i1.

case 3: a) It calls the default constructor for i2. b) It calls copy constructor for c and assigns to i1.

case 4: calls the assignment operator of class A.

I stepped into the code with Eclipse IDE but still can't come to a conclusive answer.

Plz accept my apologies if I made any mistakes in explaining things as this is my first
post but will improve subsequently.

Upvotes: 2

Views: 131

Answers (1)

nyrl
nyrl

Reputation: 779

  • case 1: I dont really understand how 10 gets assigned to _n as the value expected in the first argument of func is an object reference. Can someone help me how this is actually working with some example.

First argument is const reference, which will be bound to temporary constructed by A(int) with arg 10. Compiler attempts to convert argument types passed to argument types expected, either by constructor or cast operator. If such constructor or cast operator exists and not marked explicit, a temporary will be constructed and passed as argument.

  • case 2: As the compiler evaluates from right to left, a) It calls the constructor (secord argument) and assigns 20 to _n. b) As the object 'a' is already constructed, it just assigns a to i1.

Who said compiler evaluates from right to left in function arguments?! Order is undefined.

Also, it does not assign a to i1, it passes constant reference (address of) a.

  • case 3: a) It calls the default constructor for i2. b) It calls copy constructor for c and assigns to i1.

Correct, with respect to remark above about assignment.

  • case 4: calls the assignment operator of class A.

Correct.

Upvotes: 2

Related Questions