Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:49:35+00:00 2026-05-26T03:49:35+00:00

class item { int i; }; vector<item> ls(3); // line 1 item i1(1); //

  • 0
class item {
    int i;
};
vector<item> ls(3);   // line 1
item         i1(1);   // line 2

ls.push_back(i1);     // line 3
  • line 1:
    • default ctr called once
    • copy ctr gets called 3 times
  • line 3:
    • copy ctr gets called 4 times

My question is in line-3, what initial 3 copy ctr are doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:49:35+00:00Added an answer on May 26, 2026 at 3:49 am

    vector ls(3);

    Your Expectation:
    You probably expected the default constructor for item to be called 3 times, Instead of the default constructor is being called once and the copy constructor 3 times.

    What Actually happens:
    It is creating a class item object and then copying it to each element of the vector. There are 3 elements and hence the 3 copy constructor calls.

    Rationale:
    One can not make any assumption how the elements are constructed by std::vector. The standard says only that there must be a default constructor and does not force the std::vector implementation to do anything else in specific in this regard.


    ls.push_back(i1);

    What Actually happens:
    The vector was initially created for 3 elements so when you push a 4th element, the first first 3 elements have to be copied to new locations,so that the vector can accomodate the new element and yet have contigious allocation, this accounts for 3 copy constructor calls. The method push_back creates a copy of the elelemnt being added and then adds it to the vector, so that results in the 4th copy constructor call, In all this results in 4 copy constructor calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Equipment { std::vector<Armor*> vEquip; Weapon* mainWeapon; int totalDefense; int totalAttack; public: unsigned int
I have a list of items as such: public class Item { public int
I am using the in-built function qsort() to sort a vector of class item
I have a base class called Item : #ifndef ITEM_H #define ITEM_H #include <ostream>
I have class say for example public class Item { int price; String name;
It's hard to summarize this as a 1 line question. I have a class
public class Item { ... } public class Order { public List<Item> Items ...
I have come to see an Installer class item in Visual studio. Why they
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I have the jquery: $(.item) which gives me all elements of class item. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.