0

I write a simple code,My question is:why item_base just call constrcut function?Should item_base call "copy construct function" ?I observe that when I create item_base2,it calls "copy construct function",but item_base doesn't call "copy construct function".what's the difference?

class Item_base {
    public:
        Item_base();
        Item_base(int);
        Item_base(const Item_base &base);
        void operator=(const Item_base &item);
        virtual ~Item_base();
};

Item_base::Item_base()
{
    cout << "construct function" << endl;
}

Item_base::Item_base(int a)
{
    cout << "arg construct function" << endl;
}
Item_base::Item_base(const Item_base &base)
{
    cout << "copy function" << endl;
}

void Item_base::operator=(const Item_base &item)
{
    cout << "= operator" << endl;
}

Item_base::~Item_base()
{
}


int main()
{
    //cout << "Hello world!" << endl;
    Item_base item_base = Item_base(1);//construct function
    Item_base item_base2 = item_base;//copy construct  function
    Item_base item_base3;
    item_base3 = item_base2;// =operator function
    return 0;
}
jrok
  • 54,456
  • 9
  • 109
  • 141
CrystalJake
  • 897
  • 2
  • 10
  • 18
  • possible duplicate of [What's the difference between assignment operator and copy constructor?](http://stackoverflow.com/questions/11706040/whats-the-difference-between-assignment-operator-and-copy-constructor) – jogojapan Jun 26 '13 at 09:30
  • Actually, this is a better duplicate: http://stackoverflow.com/questions/2847787/constructor-or-assignment-operator – jogojapan Jun 26 '13 at 09:32
  • I believe in the first case the constructor of `Item_base(1)` is called and then the compiler just optimises out the copying of a temporary object here. – Kane Jun 26 '13 at 09:33

5 Answers5

5

It's called "copy elision" or "copy constructor elision". The C++ standard permits the implementation to omit certain copies. The copy from the temporary object Item_base(1) to the variable item_base is one such copy.

The same applies to moves in C++11.

So, when you define item_base, in your implementation it is simply constructed using the argument 1, instead of a temporary being constructed and then copied. All worthwhile compilers implement copy elision, although if you disable it with compiler options you would see two constructors called here.

When you define item_base2, item_base must be copied since there is no other way available to initialize item_base2.

When you define item_base3 it is constructed with no arguments.

When you assign to item_base3 it already exists, and so there is of course no construction. The assignment operator is called.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
0

item_base don't use the copy constructor because the object is already constructed. You're just assigning item_base3, you don't re-create it.

nouney
  • 4,363
  • 19
  • 31
0

A copy constructor is called when you COPY an already constructed object to another NEW object being created. The call is implicit.

Item_base z;
Item_base x = z; //Copying `z` into `x`

An assignment operator is used for explicit assignment. Not necessarily while constructing;

Item_base y;
y = x;
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

To be completely clear, the copy constuctor is called for both

Item_base a2 = a1;

Item_base a2(a1);

The assignment operator is not called for either.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Copy Constructor deals with creation a new object by using existing object, whereas Assignment operator works between two existing objects i.e just assigning values of one object to other.

Ex:(copy constructor)

Item_base ib1;
Item_base ib2 = ib1; or Item_base ib2(ib1);

Ex:(Assignment Operator)

Item_base ib1;
Item_base ib2
ib1=ib2;
Sripooja
  • 49
  • 1
  • 9