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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:10:35+00:00 2026-05-25T18:10:35+00:00

class Test{ public : int x; Test() { x = 0; cout<<"constructor with no

  • 0
class Test{
    public :
        int x;  
        Test()
        {
            x = 0;
            cout<<"constructor with no arguments called"<<endl;
        }
        Test(int xx)
        {
            x = xx;
            cout<<"constructor with single int argument called"<<endl;
        }
        
};


int main()
{
    Test a(10);
    Test aa = 10;
}

output:
Program compiles and outputs

constructor with single int argument called

constructor with single int argument called

But now

class Test{
    public :
        int x;  
        Test()
        {
            x = 0;
            cout<<"constructor with no arguments called"<<endl;
        }
        Test(int xx)
        {
            x = xx;
            cout<<"constructor with single int argument called"<<endl;
        }
        
        Test( Test& xx)
        {
            x = xx.x;
            cout<<"copy constructor called"<<endl;
        }
        
        
};


int main()
{
    Test a(10);
    Test aa = 10;
}

compilation fails.

constructorinvokings.cc:36:7: error: no viable constructor copying variable of type 'Test'
        Test aa = 10;
             ^    ~~
constructorinvokings.cc:23:3: note: candidate constructor not viable: no known conversion from 'Test' to 'Test &' for 1st
      argument
                Test( Test& xx)
                ^
1 error generated.

I am new to C++.

Aren’t Test a(10) and Test aa = 10; identical ?

why is the addition of copy constructor conflicting with Test aa=10?

if I modify Test(Test& xx) to Test(const Test& xx) it is working. But why is the compiler checking for copy constructor signature when we are trying to call constructor with integer argument.

Please clarify

Thanks in advance.

  • 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-25T18:10:36+00:00Added an answer on May 25, 2026 at 6:10 pm

    All of the answers you’ve gotten so far are overly complicated and/or slightly misleading.

    In the first construction/initialization:

    T a(10);
    

    The very obvious thing happens. The second construction/initialization is more interesting:

    T aa = 10;
    

    This is equivalent to:

    T aa(T(10));
    

    Meaning that you create a temporary object of type T, and then construct aa as a copy of this temporary. This means that the copy constructor is called.

    C++ has a default copy constructor that it creates for a class when you have none explicitly declared. So even though the first version of class T has no copy constructor declared, there still is one. The one the compiler declares has this signature T(const T &).

    Now in your second case where you declare something that looks like a copy constructor, you make the argument a T &, not a const T &. This means that the compiler, when trying to compile the second expression, tries to use your copy constructor, and it can’t. It’s complaining that the copy constructor you declared requires a non-const argument, and the argument it’s being given is const. So it fails.

    The other rule is that after the compiler has converted your initialization to T aa(T(10)); it is then allowed to transform it to T aa(10);. This is called ‘copy elision’. The compiler is allowed, in certain circumstances, to skip calls to the copy constructor. But it may only do this after it verifies that the expression is correctly formed and doesn’t generate any compiler errors when the copy constructor call is still there. So this is an optimization step that may affect exactly which parts of a program run, but cannot affect which programs are valid and which ones are in error (at least from the standpoint of whether or not they compile).

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

Sidebar

Related Questions

Consider following class class test { public: test(int x){ cout<< test \n; } };
#include <iostream> #include <exception> using std::cout; using std::endl; class test { public: test() {
say I have: class Test { public static int Hello = 5; } This
Lets say i have this usercontrol public class test : UserControl { public int
I have an entity and its mapping: public class Test { public virtual int
class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT
The following code using System.Threading; class Test { volatile int counter = 0; public
public class Test { public static void main(String[] args) { } } class Outer
I have this code #include <iostream> using namespace std; class Test{ public: int a;
I wrote the following program to test when the copy constructor is called and

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.