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 6708289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:44:28+00:00 2026-05-26T07:44:28+00:00

When I initialize an object of my class, both the default and copy constructor

  • 0

When I initialize an object of my class, both the default and copy constructor are called.

class A
{
public:
   A (string s) { str = string (s); cout << "default" << endl; }
   A (int n) { cout << "A (int n)" << endl; }
   A (string s, int n) { cout << "A (string s, int n)" << endl; }
   A (int n2, string s2) { cout << "A (int n2, string s2)" << endl; }
   A (const A& a) { str = a.str; cout << "copy" << endl; }

   inline void printA () { cout << str << endl; }

   string str;
};


int main (void)
{
   A a_1 = A ("con 1");
   cout << endl;

   A a_2 = "con 2";
   cout << endl;

   A a_3 = A (4);

   A a_4 = A ("a_4", 10);
   cout << endl;

   A a_5 = A (11, "a_5");
   cout << endl;

   cin.get();
   return 0;
}

Result:

default
copy

default

A (int n)

A (string s, int n)
copy

A (int n2, string s2)
copy

Why do a_1, a_3, and a_4 call both the default and the copy constructors?
A_3 also has a single argument, but it doesn’t need copy constructor.

  • 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-26T07:44:29+00:00Added an answer on May 26, 2026 at 7:44 am

    A a_1 = A (“con 1”);

    Constructs a temporary object by calling the constructor which takes a string as an argument,since the passed type is const char * the compiler has to perform an implicit conversion first to string() and then uses this temporary object to copy construct a new a_1 object.
    Since there is an additional implicit conversion the compiler cannot optimize this and needs to make the call to the copy constructor.
    Based on the comments and further research I am doubtful if the(above italicized)reasoning is correct.

    @David suggests in his comments:
    the copy can not be elided not because of implicit conversion, but rather because the conversion is explicit. That is, the compiler cannot optimize it because the code explicitly request the creation of the temporary and the copy construction.

    However, neither @David nor Me are able to substantiate it through a Standard Citation.

    A a_2 = “con 2”;

    constructs object a_2 by calling the appropriate constructor which takes string as an argument.

    A a_3 = A (4);

    The note in Case 1 also applies here:
    Should Ideally Construct a temporary object by calling the constructor which takes integer as an argument and then use this temporary object to copy construct a new a_3 object as in case 1, but the compiler can optimize and directly construct the object by calling constructor which takes integer as one is available.

    A a_4 = A (“a_4”, 10);

    Constructs a temporary object by calling the constructor which takes string and integer as an argument and then uses this temporary object to copy construct a new a_4 object.

    A a_5 = A (11, “a_5”);

    Constructs a temporary object by calling the constructor which takes integer and string as an argument and then uses this temporary object to copy construct a new a_5 object.

    Note that you do not have an Default constructor defined for your class.

    You can achieve the same in a more efficient manner by avoiding the creation of the temporary and then copy constructing an object in above cases by not using the assignment(=).

    A a_1("con 1");
    A a_2("con 2");
    A a_3(4);
    A a_4("a_4", 10);
    A a_5(11, "a_5");
    

    My Initial answer was on attempt to explain the behavior but as I compile this on gcc-4.3.4 on Ideone, I find that gcc is intelligent enough to optimize the copy constructor call.None of the cases invoke the copy constructor.

    The conclusion I come to is, Each compiler depending on its intelligence can or cannot optimize copy constructor calls in such as case, While the Standard does not require the compiler to perform such optimization each compiler evaluates such expressions depending on their capabilities.

    If I am wrong on this please free to add me an comment with reasoning.

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

Sidebar

Related Questions

I have an object now: class Items attr_accessor :item_id, :name, :description, :rating def initialize(options
I want to create an object, let's say a Pie. class Pie def initialize(name,
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
I recently noticed a class in C++0x that calls for an explicit default constructor.
Is it a good practice to initialize variables, specially object references at class level?
To value initialize an object of type T , one would do something along
When {0} is used to initialize an object, what does it mean? I can't
What is wrong with this Anonymous Object Initialize syntax? If (Not row Is Nothing)
I saw this reply from Jon on Initialize generic object with unknown type :
How do I go about if I need to initialize an object's base with

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.