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

  • Home
  • SEARCH
  • 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 7847115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:47:28+00:00 2026-06-02T17:47:28+00:00

I tested the following code: #include <iostream> #include <vector> class foo { public: int

  • 0

I tested the following code:

#include <iostream>
#include <vector>

class foo {
public:
    int m_data;
    foo(int data) : m_data(data) {
        std::cout << "parameterised constructor" << std::endl;
    }
    foo(const foo &other) : m_data(other.m_data) {
        std::cout << "copy constructor" << std::endl;
    }
};

main (int argc, char *argv[]) {
    std::vector<foo> a(3, foo(3));
    std::vector<foo> b(4, foo(4));
    //std::vector<foo> b(3, foo(4));
    std::cout << "a = b" << std::endl;
    a = b;
    return 0;
}

I get

   parameterised constructor
   copy constructor
   copy constructor
   copy constructor
   parameterised constructor
   copy constructor
   copy constructor
   copy constructor
   copy constructor
   a = b
   copy constructor
   copy constructor
   copy constructor
   copy constructor

However, if I replace std::vector<foo> b(4, foo(4)); by std::vector<foo> b(3, foo(4)); the copy constructor is not called by a = b and the output is

parameterised constructor
copy constructor
copy constructor
copy constructor
parameterised constructor
copy constructor
copy constructor
copy constructor
a = b

Why is in this case the copy constructor not called?

I’m using g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

  • 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-06-02T17:47:30+00:00Added an answer on June 2, 2026 at 5:47 pm

    In the first case, a needs to grow when you assign to it, which means that all its elements must be reallocated (and therefore destructed and constructed).

    In the second case, a does not need to grow, hence the assignment operator is used.

    See http://ideone.com/atPt9; adding an overloaded copy assignment operator that prints a message, we get the following for the second example:

    parameterised constructor
    copy constructor
    copy constructor
    copy constructor
    parameterised constructor
    copy constructor
    copy constructor
    copy constructor
    a = b
    copy assignment
    copy assignment
    copy assignment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please look at the following code listing: #include <iostream> using namespace std; class Base
Consider the following code: #include<iostream> #include<utility> struct Base { int baseint; }; struct Der1
In the following code : #include <iostream> #include <fstream> #include <string> using namespace std;
I have this C++ file: #include <iostream> int main(int argc, char *argv[]) { std::cout
How does the following work? #include <limits> int main() { const int* const foo
Why doesn't following code work correctly in FireFox 3.6? I have tested in IE7,
Would someone kindly explain why the following bit of code works, I've tested it
The following code seems to always generate wrong result. I have tested it on
Here I have written code for finding median of two sorted arrays: #include<iostream> using
I have the following code that I have tested and works: using (new Impersonator(Administrator,

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.