1

I am creating a vector with class objects as below.

#include <iostream>
#include <vector>

using namespace std;

class myclass
{
    public:

        myclass(int a = 0) : x(a)
        {
            cout << "myclass constructor" << endl;
        }

        int x;
};

int main()
{
    vector<myclass> v(2, 1);

    cout << "size : " << v.size() << endl;
    cout << v[0].x << endl;
    cout << v[1].x << endl;
}

As per my understanding 2 objects will be created with value '1'. But constructor is getting called only once. When I print the values of the objects, both objects are printing x values as '1'. Below is the output.

myclass constructor
size : 2
1
1

I couldn't understand why constructor is not getting called twice.

1) Is copy constructor getting called here?

I tried to write copy constructor in the class as below

myclass(myclass &obj)
{
    x = obj.x;
    cout << "Copy Constructor" << endl;
}

but it is throwing following errors while compiling.

vector.cpp:15:9: note: no known conversion for argument 1 from ‘const myclass’ to ‘myclass&’ vector.cpp:10:9: note: myclass::myclass(int) myclass(int a = 0) : x(a) ^ vector.cpp:10:9: note: no known conversion for argument 1 from ‘const myclass’ to ‘int’

2) Is there any rule that we should not define copy constructor if we are going to create vector of objects of that class? What are the rules we should follow if we are creating vector with user defined class objects?

7
  • The proper copy constructor would be myclass(const myclass &obj). The const part is quite important. Commented Oct 31, 2017 at 17:45
  • Your copy constructor is not good. The copy CTOR needs to take a constant reference of a myclass object. Not a reference. Commented Oct 31, 2017 at 17:45
  • @rodrigo: Yes. I agree. But that shouldn't impact this problem. Commented Oct 31, 2017 at 17:46
  • @kadina Well, the answer below disagree with what you just said Commented Oct 31, 2017 at 17:46
  • 1
    Items are added to vectors by copying them. Commented Oct 31, 2017 at 17:46

2 Answers 2

4

The copy constructor requires a const reference. Use myclass(const myclass &obj) for the copy constructor.


As per my understanding 2 objects will be created with value '1'. But constructor is getting called only once.

The constructor is called once and then the object is copied a number of times.


Then the picture is clear from the output:

myclass constructor
Copy Constructor
Copy Constructor
size : 2
1
1

Remember that the vector will also resize and have to copy the elements sometimes.

Sign up to request clarification or add additional context in comments.

7 Comments

Does this mean one object will be created and remaining objects will be copy constructed?
It definitely looks so
And are there any rules we should follow if we are creating vector with user defined class objects?
@kadina: Sure there is! The rule of three or the rule of five.
@kadina You only need to implement operator==() if you intend to compare vectors with ==. If you try an operation with vectors and fail to implement the proper operation on your class you will get a compiler error.
|
1

try

myclass(const myclass &obj)
    {
        x = obj.x;
        cout << "Copy Constructor" << endl;
    }

As for part 2, general rule apply : create copy constructor where default member-by member copying will fail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.