1

My question is for the last statement i.e. before return 0;

Why the parametrize constructor is being called when we are trying to assign an int value to an object.

My piece of code:

#include<iostream>
using namespace std;
class Test {
private:
    int i;
    public:
    Test(int s=0):i(s) {
            cout<<"param ctor: "<<i<<endl;
    }
};

int main()
{
    Test a;         //param ctor called
    Test b(5);      //param ctor called
    //b(a);         //error as we have not implemented copy ctor
    b=a;            //compiler provided assignment opr. called
    b=100;          //why param ctor called for this.??
    return 0;
}

OUTPUT:

  param ctor: 0
  param ctor: 5
  param ctor: 100
Prabhat Kumar Singh
  • 1,711
  • 15
  • 20
  • 5
    Make your constructor `explicit` to disallow `b = 100`. – Jarod42 Mar 10 '16 at 14:46
  • your comments do not mtch your code. The copy constructor is not being called in `b=100;`. It should be a constructor call plus an assignment. – NathanOliver Mar 10 '16 at 14:48
  • 1
    `Test b(a);` would be correct (copy constructor). on the other side, `b(a);` would call `operator()(/*const*/Test&)` (but not present). – Jarod42 Mar 10 '16 at 14:48
  • Of what datatype is 100 really? We might assume int, but I guess the compiler will try to convert the 100 to something that can be used with that class. Since you have a constructor that accepts an integer and you have a number I imagine that it uses that one and really assignes constructed Test object in between which leads to triggering your defined constructor. – Gerhard Stein Mar 10 '16 at 14:52
  • @NathanOliver yes that was mistake, My intension was to write param ctor there thanks for pointing out. – Prabhat Kumar Singh Mar 10 '16 at 14:54
  • This is similar to a question I asked a while ago: http://stackoverflow.com/questions/30322423/why-can-i-assign-a-qobject-to-a-qobject . The second sentence in the answer basically explains what happens here. – anderas Mar 10 '16 at 15:04

4 Answers4

7

The reason is simple: every class X has a copy constructor (a consturctor taking X) and a copy assignment operator (an assignment operator taking X). If you don't declare these yourself, the compiler declares them for you implicitly. In some cases, they are defined as deleted (which means it is an error to use them), but they're always there.

So when you do this:

b = 100;

it's effectively translated to this:

b.operator=(100)

and the best matching overload of operator= is searched for. There's only one overload, actually: the implicitly declared copy-assignment operator Test& Test::operator=(const Test &), so the compiler checks if it could convert the argument 100 to the assignment operator's parameter type const Test &. And it turns out that it can, thanks to the converting constructor Test::Test(int), so that is what ends up called.

If you want to disable such behaviour, you can mark the constructor as explicit. This will prevent it from being used for implicit conversions, such as the one converting 100 to the type of the assignment operator's parameter.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

Your constructor Test(int s=0) can be used as a conversion operator from int to Test. When b = 100 is evaluated, 100 is converted to Test, and than default assignment operator is invoked.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

When you do b=100 what basically happens is that the compiler generates b.operator=(Test(100))

The constructor call you see is from that Test(100).

If you make the constructor explicit then the compiler can't use the constructor to implicitly convert int values to Test objects, and you would get an error.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
-1

I am not sure on this but,

I came to know that compiler first searches for the matching function call for..

b=100;

i.e. an overloaded assignment operator function for class A which takes int as parameter and when It fails to find any suitable match it directs the call to parametrize constructor.

which in turn do the rest of the assignment job.

Please fill free to correct me if I am wrong or if I am missing any detail here.

Prabhat Kumar Singh
  • 1,711
  • 15
  • 20
  • Random set of words. – SergeyA Mar 10 '16 at 14:49
  • @SergeyA I already stated that I am not sure, on whats actually going on there I heard these from my cpp faculty that something like this is done by compiler. But I needed full explanation of whats the reason and solution for such behaviour of compiler. – Prabhat Kumar Singh Mar 10 '16 at 15:04
  • 1
    If you are not sure, please ask question rather than providing random answers. – SergeyA Mar 10 '16 at 15:05
  • 1
    @SergeyA: when Hindi is transliterated into English directly, it looks a little random and a bit like yoda-speak. Wouldn't be so hard on non-English native speakers! – CarlH Mar 10 '16 at 15:05