Ben
Ben

Reputation: 563

C++ Copy Constructor invocation

Quick question. If I have an array and have properly overloaded the assignment operator, then when I do something like this:

A = B 

When A and B are both objects of type array, am I calling the copy constructor, or just the overloaded assignment operator(=)?

I know that a copy constructor is called when

  1. Pass By value
  2. return a value of class type
  3. when an object is being declared and initialized by another object of the same type given in parenthesis.

3 above makes me confused and thinking that A = B is also calling the copy constructor.

Is it just calling the overloaded assignment operator?

Thanks!

Upvotes: 0

Views: 2837

Answers (5)

Cechner
Cechner

Reputation: 859

3 above makes me confused and thinking that A = B is also calling the copy constructor.

Unless your copy constructor is declared explicit, the following will indeed call the copy constructor:

MyType A = B; // will call copy ctor

I believe the standard guarantees this (I don't have my reference handy) so you know you will not be calling the default ctor followed by the assignment operator.

Upvotes: 0

Tamer Shlash
Tamer Shlash

Reputation: 9523

Copy constructor is called -as you said- when

  1. Pass By value
  2. return a value of class type
  3. when an object is being declared and initialized by another object of the same type given in parenthesis or as an assignment.

Consider the output of the following code :

#include <iostream>

using std::cout;

class Test
{
public:
    Test()
    {
    }
    Test(const Test& T)
    {
        cout << "Calling Copy Constructor";
    }
};

void PassingValue(Test T)
{
    cout << " while Passing parameter by value\n\n";
}

void PassingReference(Test &T)
{
    cout << "NOT CALLING copy constructor while Passsing Parameter by reference\n\n";
}

int main()
{
    Test One;
    // case 1 :
    cout << "case 1 : \n";
    Test Two(One);
    cout << " while creating Two as a copy of One\n\n";
    // case 2 :
    cout << "case 2 : \n";
    PassingValue(One);
    // case 3 :
    cout << "case 3 : \n";
    Test Three = Two;
    cout << " while assigning initial value\n\n";
    // case 4 :
    cout << "case 4 : \n";
    PassingReference(Two);
    // case 5
    cout << "case 5 : \n";
    Test Four;
    Four = Three;
    cout << "NOT CALLING copy constructor while assigning normally\n\n";
    return 0;
}

Upvotes: 0

Adam Hawes
Adam Hawes

Reputation: 5449

Since you've said the array is your own class with an overloaded assignment operator then you've already answered your own question.

The copy constructor is literally only called when you are constructing the object from another:

Obj a; Obj b(a);

It might wind up being called by some sort of compiler magic if you do:

Obj a; Obj b = a;

But I never bothered to actually look that up.

If you just do a = b you are not constructing a, therefore you'd not call the copy constructor.

Make sense?

Upvotes: 1

Mahesh
Mahesh

Reputation: 34615

Overloaded Assignment Operator is called if performed A=B;

class userDefinedArray
{
    int size ;
    int* ptr;

    public:
    userDefinedArray( int size ) :   size(size)
                                   , ptr( new int[size] )
    {}
    // The Big Three = 1. Copy Constructor 2. Assignment Operator 3. Destructor
};

If the above is the class definition, then assignment operator should be called.

Upvotes: 0

GManNickG
GManNickG

Reputation: 503795

None of the above: you cannot assign arrays.


If you have your own array class, and it has something like this:

struct arr
{
    arr& operator=(const arr& other)
    {
        // ...
    }
};

arr a, b;

Then these are equivalent:

a = b;
a.operator=(b);

It's just like calling a function.

Upvotes: 3

Related Questions